Php PDO多个命名占位符无法检索数据
如果准备好的语句只有一个指定的占位符,那么到目前为止我编写的代码可以正常工作,但是如果查询有多个条件,它不会从数据库返回任何结果 例如:Php PDO多个命名占位符无法检索数据,php,mysql,pdo,Php,Mysql,Pdo,如果准备好的语句只有一个指定的占位符,那么到目前为止我编写的代码可以正常工作,但是如果查询有多个条件,它不会从数据库返回任何结果 例如: $query = array(); $query['columns'] = array('*'); $query['tables'] = array('esl_comments'); $query['where'] = array( 'esl_comments.commentVisible' => array('=', 'Y') ); 很好。但
$query = array();
$query['columns'] = array('*');
$query['tables'] = array('esl_comments');
$query['where'] = array(
'esl_comments.commentVisible' => array('=', 'Y')
);
很好。但如果我尝试:
$query = array();
$query['columns'] = array('*');
$query['tables'] = array('esl_comments');
$query['where'] = array(
'esl_comments.commentVisible' => array('=', 'Y'),
'esl_comments.commentID' => array('=', '1'),
);
(注意附加的commentID参数)尽管mySQL数据库中存在满足条件的数据,但它无法返回任何内容
我编写的PDO代码是:
$sql ='SELECT ';
foreach($query['columns'] as $column){ //What columnns do we want to fetch?
$sql.=$column . ", ";
}
$sql = rtrim($sql, " ,");
$sql .=' FROM '; //Which tables will we be accessing?
foreach($query['tables'] as $tables){
$sql.=$tables . ", ";
}
$sql = rtrim($sql, " ,"); //Get rid of the last comma
$sql .=' WHERE ';
if(array_key_exists('where', $query)) //check if a where clause was provided
{
$fieldnames = array_keys($query['where']);
$count = 0;
$size = sizeof($fieldnames);
$bindings = array();
foreach($query['where'] as $where){
$cleanPlaceholder = str_replace("_", "", $fieldnames[$count]);
$cleanPlaceholder = str_replace(".", "", $cleanPlaceholder);
$sql.=$fieldnames[$count].$where[0].":".$cleanPlaceholder." AND ";
$bindings[$cleanPlaceholder]=$where[1];
$count++;
}
$sql = substr($sql, 0, -5); //Remove the last AND
}
else{ //no where clause so set it to an always true check
$sql.='1=1';
$bindings=array('1'=>'1'); //Provide default bindings for the statement
}
$sql .= ';'; //Add the semi-colon to note the end of the query
echo $sql . "<br/><br/>";
// exit();
$stmt = $this->_connection->prepare($sql);
foreach($bindings as $placeholder=>$bound){
echo $placeholder . " - " . $bound."<br/>";
$stmt->bindParam($placeholder, $bound);
}
$result = $stmt->execute();
echo $stmt->rowCount() . " records<br/>";
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
被绑定的参数如下所示:
SELECT * FROM esl_comments WHERE esl_comments.commentVisible=:eslcommentscommentVisible AND esl_comments.commentID=:eslcommentscommentID;
eslcommentscommentVisible - Y
eslcommentscommentID - 1
bindParam需要一个引用
这个问题是由在foreach循环中绑定参数的方式引起的
foreach($bindings as $placeholder=>$bound){
echo $placeholder . " - " . $bound."<br/>";
$stmt->bindParam($placeholder, $bound);
}
传递值
使用bindValue
代替bindParam
:
foreach($bindings as $placeholder => $bound) {
$stmt->bindValue($placeholder, $bound); // bind the value to the statement
}
bindParam需要一个引用
这个问题是由在foreach循环中绑定参数的方式引起的
foreach($bindings as $placeholder=>$bound){
echo $placeholder . " - " . $bound."<br/>";
$stmt->bindParam($placeholder, $bound);
}
传递值
使用bindValue
代替bindParam
:
foreach($bindings as $placeholder => $bound) {
$stmt->bindValue($placeholder, $bound); // bind the value to the statement
}
显示有效查询和无效查询的最终SQL字符串。如果没有,也显示错误消息。显示有效和无效查询的最终SQL字符串。当错误消息不存在时,也显示错误消息。太好了!我没有意识到bindValue()和bindParam()之间的区别。这为我解决了问题。非常感谢,太好了!我没有意识到bindValue()和bindParam()之间的区别。这为我解决了问题。非常感谢你。