直到最近我才使用这种准备好的语句方法,但我遇到了问题。我在 stackoverflow 上读到了一些类似的问题,但它们似乎指的是不同的东西,比如重复标记(我正在使用 ? 不是 :name 所以我认为这不适用)并且我在 phpstorm 中使用了 xdebug 来调试什么正在通过。
该查询是我在脚本中使用的过滤器的一部分。
调试器中的查询字符串显示:
SELECT * FROM data_table WHERE (hud_game_type = ? OR hud_game_type = ? OR hud_game_type = ?) AND (hud_table_type = ? OR hud_table_type = ? OR hud_table_type = ?) AND (hud_table_size = ? OR hud_table_size = ? OR hud_table_size = ? OR hud_table_size = ?) AND approve = ? ORDER BY hud_downloads DESC
那么显然有 11 x ?
调试器中的$search_values
显示:
0 = "Omaha"
1 = "Holdem"
2 = "All"
3 = "Cash"
4 = "Tourney"
5 = "All"
6 = "6max"
7 = "FR"
8 = "HU"
9 = "All"
10 = "1"
再次明确数组中的 11 个值,这是代码的最后部分:
这是代码,但如上所述,构建的字符串在上面,它似乎是合法的,因为该代码依赖于在另一个函数中构建的表单中传递的值,我可以添加,如果相关,但我不会'我不这么认为,因为我显示了上面输出的查询
$result = $database->resultset("
SELECT * FROM $data_table WHERE $search_game_type AND $search_table_type AND $search_table_size AND approve = ? $search_sort_by",(array($search_values)));
// returns an array of the results, first we execute and then fetch the results
public function resultset($query,$values){
$this->stmt = $this->dbh->prepare($query);
try{
// this handles situation where no params need to be escaped.
if($values == ""){
$this->stmt->execute();
} else{
$this->stmt->execute($values);
}
} catch (PDOException $e){
$this->error_db_query_failed(true,$values,$e->getMessage(),"Error #12");
}
现在PDO返回的错误是:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
这个相同的结果集函数似乎在代码的其他部分工作得很好,但我只在其中传递一两个参数。
即使有人可以给我指出正确的方向或建议错误可能是什么,我很乐意进一步搜索,但许多其他错误都涉及 :name 类型绑定(bind),因此过程是不同的。
最佳答案
由于 $search_values
已经是一个数组,因此您应该将其直接传递给函数,不要使用 array($search_values)
。
关于php - 绑定(bind)变量的数量与标记的数量不匹配,而标记的数量似乎匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21767739/