在 PHP 函数中包装 MySQL 查询
Posted
技术标签:
【中文标题】在 PHP 函数中包装 MySQL 查询【英文标题】:Wrapping MySQL query inside a PHP function 【发布时间】:2017-03-27 18:30:57 【问题描述】:我有两个问题
1.) 下面的代码可以工作,但我需要在每个函数中包含数据库连接。可以不加require("db.php");
全局调用db.php
functions.php
function GetUsers($id, $active)
require("db.php");
$result = $mysqli->query("SELECT * FROM users WHERE id = '$id' and active = '$active' ") or trigger_error(mysql_error());
return $result;
index.php
include("functions.php");
$id = 2;
$active = 1;
$row = GetGetUsers ($id, $active);
foreach($row as $users)
echo '<h1>'. $users['username']. '</h1>';
echo '<p>'. $users['email']. '</p>';
2.) 我用 mysql 准备好的语句尝试了相同的代码,但根本不起作用
functions.php
function GetAccounts($id, $active)
require("db.php");
$result = $mysqli->prepare("SELECT * FROM users WHERE id = ? and active = ? ") or trigger_error(mysql_error());
$result->bind_param('ii', $id, $active);
$result->execute();
$result->close();
return $result;
db.php
$mysqli = new mysqli(); $mysqli->connect('localhost', 'root', 'mysql', 'function_test');
非常感谢任何解决我的两个问题的 cmets 和答案。
【问题讨论】:
您可以改用类 (OOP) 和自动加载(如 PSR-4)。 你db.php
中的代码是什么?
@MarkVincentManjac 这里是数据库代码$mysqli = new mysqli(); $mysqli->connect('localhost', 'root', 'mysql', 'function_test'); //Hostname - normaly localhost //DBusername - your mysql username //DBpassword - your mysql password //DBname - your mysql database name if ($mysqli->connect_errno) echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
为什么要在 mysqli 中使用 mysql_error?
【参考方案1】:
这是我在整个项目中使用的一个类。试试这个
<?php
class access_db
function Connect()
$this->con= new mysqli("localhost","accoladets","accolad3TS", "accoladets") or die(mysqli_error());
function eClose()
mysqli_close($this->con);
/**Insert data
* @param string $table name of the table
* @param string $field table field eg. "field1,field2,field3"
* @param string $param field parameters subtitute eg. "(?,?,?)"
* @param array $valarr array of field values eg. array("string1",2,"string3") or array(array(),array())
* @param string $option (optional)
* Available option: <b>get_id</b> - (for single insert only)get the last auto generated id after query.
* @param string $option2 (optional). multi or NULL
* @return boolean true, error message or the expected value of the given option
*/
function insert_data($table,$field,$param,$valarr,$option = NULL,$option2 = NULL)
$this->Connect();
$sql = "INSERT INTO ".$table." (".$field.") VALUES ".$param;
if($stmt = $this->con->prepare($sql))
if($option2 == 'multi')
$idarr = array();
foreach($valarr as $v)
$this->dynamic_bind_param($stmt, $v);
if($stmt->execute())
if(is_null($option) == true)
$idarr = true;
else
if($option == 'get_id')
$idarr[] = $stmt->insert_id;
else
return '<b>(INSERT Error)</b> Error in executing the prepared statement: '.htmlspecialchars($stmt->error);
exit();
return $idarr;
exit();
else
$this->dynamic_bind_param($stmt, $valarr);
if($stmt->execute())
if(is_null($option) == true)
return true;
else
if($option == 'get_id')
return $stmt->insert_id;
else
return '<b>(INSERT Error)</b> Error in executing the prepared statement: '.htmlspecialchars($stmt->error);
exit();
else
return '<b>(INSERT Error)</b> Error in initializing the prepared statement: '.htmlspecialchars($stmt->error).'SQL: '.$sql.' VALUES: '.json_encode($valarr);
exit();
$stmt->close();
$this->eClose();
/**Update data
* @param string $table name of the table
* @param string $field table field eg. "field1 = ?, field2 = ?, field3 = ?"
* @param string $where database filter eg. "filter1 = ? AND filter2 = ?" or ""
* @param array $valarr array of field values eg. array("string1",2,"string3")
* @return boolean true or error message
*/
function update_data($table,$field,$where,$valarr)
$this->Connect();
$where = ($where == '')? '' : 'WHERE '.$where;
$sql = "UPDATE ".$table." SET ".$field." ".$where;
if($stmt = $this->con->prepare($sql))
$this->dynamic_bind_param($stmt, $valarr);
if($stmt->execute())
return true;
else
return '<b>(UPDATE Error)</b> Error in executing the prepared statement: '.htmlspecialchars($stmt->error).'SQL: '.$sql.' Values: '.json_encode($valarr);
exit();
else
return '<b>(UPDATE Error)</b> Error in initializing the prepared statement: '.htmlspecialchars($stmt->error).'SQL: '.$sql;
exit();
$stmt->close();
$this->eClose();
/**Delete data
* @param string $table name of the table
* @param string $where database filter eg. "filter1 IN (1,2,3) AND filter = 4"
* @return boolean true or error message
*/
function delete_data($table,$where = NULL)
$this->Connect();
$where = (is_null($where) == true)? '' : 'WHERE '.$where;
$sql = "DELETE FROM ".$table." ".$where;
$qry = $this->con->query($sql);
if($qry)
return true;
else
return '<b>(Delete Error)</b> Error executing the delete query: '.htmlspecialchars($this->con->error);
$this->eClose();
/**Get table data
* @param string $table name of the table
* @param string $field table field eg. "field1, field2, field3"
* @param string $orderby database filter eg. "field1 ASC" or NULL
* @param string $where database filter eg. "filter1 = ? AND filter2 = ?" or NULL
* @param array $valarr array of field values eg. array("string1",2,"string3") or NULL
* @return array Multidimensional_Arrays Two-dimensional Arrays
*
* Array([0] => Array([field1] => string1,[field2] => 2,[field3] => string3))
*/
function get_table_data($table,$field,$orderby = NULL,$where = NULL,$valarr = NULL)
$this->Connect();
$fields = array();
$results = array();
$orderby = (is_null($orderby) == true)? '' : 'ORDER BY '.$orderby;
$where = (is_null($where) == true)? '' : 'WHERE '.$where;
$sql = "SELECT ".$field." FROM ".$table." ".$where." ".$orderby;
if($stmt = $this->con->prepare($sql))
if(is_null($valarr) == false)
$this->dynamic_bind_param($stmt, $valarr);
if($stmt->execute())
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field())
$var = $field->name;
$$var = null;
$fields[$var] = &$$var;
call_user_func_array(array($stmt,'bind_result'),$fields);
$i = 0;
while ($stmt->fetch())
$results[$i] = array();
foreach($fields as $k => $v)
$results[$i][$k] = $v;
$i++;
return $results;
else
return '<b>(SELECT Error)</b> Error in executing the prepared statement: '.htmlspecialchars($stmt->error).'SQL: '.$sql;
else
return '<b>(SELECT Error)</b> Error in initializing the prepared statement: '.htmlspecialchars($stmt->error).'SQL: '.$sql;
$stmt->close();
$this->eClose();
private function dynamic_bind_param($stmt,$values)
if(is_array($values) == true)
$types = '';
foreach($values as $param)
// set param type
if (is_string($param))
$types .= 's'; // strings
else if (is_int($param))
$types .= 'i'; // integer
else if (is_float($param))
$types .= 'd'; // double
else
$types .= 'b'; // default: blob and unknown types
$paramArr[] = &$types;
for ($i=0; $i<count($values);$i++)
$paramArr[] = &$values[$i];
call_user_func_array(array($stmt,'bind_param'), $paramArr);
else
if (is_string($values))
$types .= 's'; // strings
else if (is_int($values))
$types .= 'i'; // integer
else if (is_float($values))
$types .= 'd'; // double
else
$types .= 'b'; // default: blob and unknown types
$stmt->bind_param($types,$values);
return $stmt;
?>
在您的 php 代码中,将此行添加到所有其他 require_once '../class/db_fn_class.php';
之上,根据您放置它的位置更改类文件的路径,然后实例化它类似于 $access_db = new access_db();
。这是一个实例化后如何使用它的示例。
$data = $access_db->get_table_data("users","field1,field2",NULL,"id = ? AND active = ?",array($id, $active));
它将以对象数组的形式返回数据,因此这里是提取数据的方法
foreach($data as $val)
$f1 = $val["field2"]; // use the name of every table field you put on the get_table_data as the array id of the $val. $val is user_define and so as $data
$f2 = $val["field2"]
【讨论】:
您是否介意在您的代码中添加一个选择示例。我试过但没有用 在你的 php 代码中添加require_once '../class/db_fn_class.php';
然后实例化它 $access_db = new access_db();
然后你可以像这样使用它 $data = $access_db->get_table_data("users","field1,field2",NULL,"id = ? AND active = ?",array($id, $active));
这将返回一个数据数组并提取数据 foreach($data as $val)$f1 = $val["field2"]; $f2 = $val["field2"]
跨度>
以上是关于在 PHP 函数中包装 MySQL 查询的主要内容,如果未能解决你的问题,请参考以下文章