gpt4 book ai didi

php - "number of bound variables does not match number of tokens"——但确实如此

转载 作者:行者123 更新时间:2023-11-29 00:19:31 31 4
gpt4 key购买 nike

我见过很多人收到错误 SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens,但据我所知,他们都没有问题对我有帮助或适用。我确信这对我来说真的很愚蠢......

当我回显 $ps$ps_val(在我的数据库类和页面上)时,它们具有相同数量的值。我什至会回显 SQL 并计数并仔细检查占位符的拼写,所有这些都会加起来。我什至进入了 phpMyAdmin,复制粘贴了我的 SQL,并复制粘贴了所有占位符及其值,这很有效,所以我知道这不是 SQL 语法。

$ps[] = ":category";
$ps_val[] = $_GET['cat'];

if (isset($_GET['color'])){
// resort_color converts hex color into comma delimited text colors
// e.g. resort_color("#000000") returns "black, black with white trim, etc."
$colors = resort_color($_GET['color']);

if (strpos($colors, ",") >= 0){
$colors = explode(",", $colors);

$i = 0;
foreach ($colors as $color){
$color = trim($color);
if ($i == 0){
$sql_color = " AND `Color` LIKE \"%:color{$i}%\"";
$ps[] = ":color{$i}";
$ps_val[] = $color;
} else {
$sql_color = $sql_color . " OR `Color` LIKE \"%:color{$i}%\"";
$ps[] = ":color{$i}";
$ps_val[] = $color;
}
$i++;
}
} else {
$sql_color = 'AND `Color` LIKE \"%:color%\"';
$ps[] = ":color";
$ps_val[] = $colors;
}
} else {
$sql_color = "";
}

$sql[] = 'SELECT * FROM `bullet_catalog` WHERE `Category` = :category ' . $sql_color;
$sql[] = 'SELECT * FROM `leeds_catalog` WHERE `Category` = :category ' . $sql_color;
$sql[] = 'SELECT * FROM `trimark_catalog` WHERE `Category` = :category ' . $sql_color;

if ($db->query_all($sql, $ps, $ps_val)){
$products = $db->query_all($sql, $ps, $ps_val);
} else {
$products = "";
}

还有我的query_all 函数:

public function query_all($sql, $ps="", $ps_val=""){
if (!$sql){
return false;
} else {
foreach ($sql as $ind_sql){
$query = $this->pdo->prepare($ind_sql);
if ($ps){
if (is_array($ps)){
$i = 0;
foreach ($ps as $pss){
$query->bindValue($pss, $ps_val[$i]);
$i++;
}
} else {
$query->bindValue($ps, $ps_val);
}
}

try {
$query->execute();

$result = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $res){
$results[] = $res;
}
} catch (PDOException $error){
echo "<div><pre>" . print_r($error->getMessage(), true) . "</pre></div>";
}
}
if (isset($results)){
return $results;
} else {
return false;
}
}
}

PDOStatement::debugDumpParams 返回

SQL: [295] SELECT * 
FROM `table`
WHERE `Category` = ":category"
AND `Color` LIKE "%:color0%"
OR `Color` LIKE "%:color1%"
OR `Color` LIKE "%:color2%"
OR `Color` LIKE "%:color3%"
OR `Color` LIKE "%:color4%"
OR `Color` LIKE "%:color5%"
OR `Color` LIKE "%:color6%"

Params: 8

Key: Name: [9] :category
paramno=-1
name=[9] ":category"
is_param=1 param_type=2

Key: Name: [7] :color0
paramno=-1
name=[7] ":color0"
is_param=1 param_type=2

Key: Name: [7] :color1
paramno=-1
name=[7] ":color1"
is_param=1 param_type=2

Key: Name: [7] :color2
paramno=-1
name=[7] ":color2"
is_param=1 param_type=2

Key: Name: [7] :color3
paramno=-1
name=[7] ":color3"
is_param=1 param_type=2

Key: Name: [7] :color4
paramno=-1
name=[7] ":color4"
is_param=1 param_type=2

Key: Name: [7] :color5
paramno=-1
name=[7] ":color5"
is_param=1 param_type=2

Key: Name: [7] :color6
paramno=-1
name=[7] ":color6"
is_param=1 param_type=2

最佳答案

如果你使用PDO bindValue要使用 LIKE 条件进行搜索,您不能将百分比和引号放在参数占位符 '%:color0%' 中。

这是错误的:

SELECT * FROM `bullet_catalog` WHERE `color` LIKE '%:color0%'

正确的解决方案是像这样只使用占位符:

SELECT * FROM `bullet_catalog` WHERE `color` LIKE :color0

然后将百分比添加到存储关键字的 php 变量中:

$query->bindValue(':color0', "%" . $color0 . "%");

另外 %_$color0 中没有转义,所以你应该自己转义它们。

也有解释

关于php - "number of bound variables does not match number of tokens"——但确实如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617150/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com