【发布时间】:2014-11-25 17:54:42
【问题描述】:
所以我在使用 PDO 执行执行查询时遇到问题。我不断从服务器返回的错误是我有一个数组到字符串的转换。然后它告诉我在execute(array()) 代码行中输入的参数数量也无效。我的错误代码中还有一个1 缓冲区,这意味着可能存在一些错误的连接。
该功能是一个搜索页面,用户可以在其中输入最多 6 个参数来搜索书籍。有 6 个 if 语句来检查是否为空,如果不为空,则将变量添加到数组 params 中,并将部分 sql 查询连接到开始语句的末尾。以下是相关代码:
$title = $_POST['title'];
$author = $_POST['author'];
$publisher = $_POST['publisher'];
$isbn = $_POST['isbn'];
$condition = $_POST['condition'];
$status = $_POST['status'];
//array to hold variables
$params = array();
$concat_stmt_orig = "SELECT * FROM Book WHERE ";
$concat_stmt = "SELECT * FROM Book WHERE ";
//Check for Title
if(!empty($title)){
//add title to the end
$concat_stmt = $concat_stmt." `Title` = ?";
//add variable to print list
array_push($params, $title);
}
//Check for Author
if(!empty($author)){
if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
//add author to the end
$concat_stmt = $concat_stmt." AND `Author1` = ?";
//add variable to print list
array_push($params, $author);
}
else{
//add author to the end
$concat_stmt = $concat_stmt." `Author1` = ?";
//add variable to print list
array_push($params, $author);
}
}
//Check for publisher
if(!empty($publisher)){
if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
//add publisher to the end
$concat_stmt = $concat_stmt." AND `Publisher` = ?";
//add variable to print list
array_push($params, $publisher);
}
else{
//add pubisher to the end
$concat_stmt = $concat_stmt." `Publisher` = ?";
//add publisher to print list
array_push($params, $publisher);
}
}
//check for ISBN
if(!empty($isbn)){
if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
//add isbn to the end
$concat_stmt = $concat_stmt." AND `ISBN` = ?";
//add variable to print list
array_push($params, $isbn);
}
else{
//add pubisher to the end
$concat_stmt = $concat_stmt." `ISBN` = ?";
//add publisher to print list
array_push($params, $isbn);
}
}
//check for condition
if(!empty($condition)){
if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
//add condition to the end
$concat_stmt = $concat_stmt." AND `BookCondition` = ?";
//add condition to print list
array_push($params, $condition);
}
else{
//add condition to the end
$concat_stmt = $concat_stmt." `BookCondition` = ?";
//add condition to print list
array_push($params, $condition);
}
}
//check for status
if(!empty($status)){
if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
//add status to the end
$concat_stmt = $concat_stmt." AND `Status` = ?";
//add variable to print list
array_push($params, $status);
}
else{
//add condition to the end
$concat_stmt = $concat_stmt." `Status` = ?";
//add condition to print list
array_push($params, $status);
}
}
//prepare statement for querying
$stmt_retrieve = $db->prepare($concat_stmt);
echo print_r($stmt_retrieve);
var_dump($params);
//exectue query
$stmt_retrieve->execute(array($params));
}
echo 和 var dump 用于测试目的,以检查 sql 语句和数组值的正确数量
这里是错误
PDOStatement 对象 ( [queryString] => SELECT * FROM Book WHERE
Title= ? ANDAuthor1= ? ) 1array(2) { [0]=> string(13) "示例标题" [1]= > string(14) "示例作者" } 注意:数组到字符串的转换在...."yadayadayada"致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY093]:无效的参数号:绑定变量的数量与令牌的数量不匹配'......“yadayadayada”
我尝试将帖子值输入为 '$title'、'$author'...等,但仍然产生错误,我也尝试了 printf 和其他。
当我尝试输入多个变量进行搜索时,我只会收到这些错误。当我只搜索一个变量时,程序没有错误,但仍然不会返回任何内容。
感谢任何帮助或指导:)
【问题讨论】:
-
使用关联数组和命名占位符可能更容易理解。此外,尝试在数组中编译您的条件,然后加入
"AND"作为最后一步。从头到尾都有双分支是大量的重复工作和自找麻烦。 -
感谢@tadman 的提示,以后一定会这样做:)