【发布时间】:2014-07-13 15:34:21
【问题描述】:
我正在尝试创建一个小型 SQL 查询类。
这是我的课程,但我不知道为什么,我有这个错误: 严格标准:在第 52 行中只能通过引用传递变量
第 52 行是:
if (!$stmt->bind_param($param[$i][0], mysqli_real_escape_string($this->mysqli, $param[$i][1]))) {
我的代码(我开始了):
<?php
class Sql{
private $db;
private $user;
private $pwd;
private $url;
private $param;
private $mysqli;
function __construct($db, $user, $pwd, $url){
$this->db = $db;
$this->user = $user;
$this->pwd = $pwd;
$this->url = $url;
}
/**
* mysqli::connection()
*
* @return
*/
public function connection()
{
try{
$this->mysqli = new mysqli($this->db, $this->user, $this->pwd, $this->url);
}catch(Exception $e){
throw new Exception("Impossible de se connecter à la base " . $this->db);
}
}
public function select($query, $param, $debug=false){
$this->connection();
$r = $this->InitialiseResult("select");
if (!($stmt = $this->mysqli->prepare($query))) {
echo "Echec de la préparation : (" . $this->mysqli->errno . ") " . $this->mysqli->error;
}
//Param
for($i=0;$i<sizeof($param);$i++){
if (!$stmt->bind_param($param[$i][0], mysqli_real_escape_string($this->mysqli, $param[$i][1]))) {
echo "Echec lors du liage des paramètres : (" . $stmt->errno . ") " . $stmt->error;
}
}
if (!$stmt->execute()) {
echo "Echec lors de l'exécution : (" . $stmt->errno . ") " . $stmt->error;
}
if (!($res = $stmt->get_result())) {
echo "Echec lors de la récupération du jeu de résultats : (" . $stmt->errno . ") " . $stmt->error;
}else{
$r["state"] = true;
$r["rows"] = $res->fetch_assoc();
$r["num_rows"] = $res->num_rows;
if($debug)
var_dump($r);
}
return $r;
}
/**
* mysqli::InitialiseResult()
*
* @param mixed $p
* @return
*/
public function InitialiseResult($p)
{
$r = array(); //on écrase
$r["state"] = false;
switch($p){
case "select":
$r["rows"] = array();
$r["num_rows"] = 0;
break;
}
return $r;
}
}
?>
我尝试将 $param 放在一个属性中并使用 mysqli_real_escape_string() 但错误仍然存在。
有什么想法吗?
【问题讨论】:
-
你不需要
mysqli_real_escape_string和准备好的语句。直接传递变量即可。 -
哇,太棒了!非常感谢!