PDO Sqlite一般错误:25绑定或列索引超出范围 [英] PDO Sqlite General error: 25 bind or column index out of range
问题描述
Read some relevant questions here, here and here. A simple query still triggers an error
SQLSTATE[HY000]: General error: 25 bind or column index out of range
$query
INSERT OR IGNORE INTO `menu` (`id`,`name`,`name_clean`,`display`) VALUES (:idInsert,:nameInsert,:name_cleanInsert,:displayInsert);
UPDATE `menu` SET id=:idUpdate,name=:nameUpdate,name_clean=:name_cleanUpdate,display=:displayUpdate WHERE id = 1;
';
$values
[:idInsert] => 1
[:idUpdate] => 1
[:nameInsert] => 2
[:nameUpdate] => 2
[:name_cleanInsert] => 3
[:name_cleanUpdate] => 3
[:displayInsert] => 1
[:displayUpdate] => 1
代码段. $this->db->handle
是数据库句柄.如上述参考文献之一所述,我已经实现了setAttribute(\PDO::ATTR_EMULATE_PREPARES, true)
以便能够执行多个查询
The snippet. $this->db->handle
is the DB handle. As stated in one of the references above, I have implemented the setAttribute(\PDO::ATTR_EMULATE_PREPARES, true)
to be able to execute multiple queries
$statement = $this->db->handle->prepare($query);
$statement->execute($values);
和这个人打了几个小时,感觉就像我在跑圈子.我在这里想念什么?
Fighting with this one for hours and feels like I am running circles. What am I missing here?
更新
根据需要定义表
DROP TABLE IF EXISTS `menu`;
CREATE TABLE `menu` (`id` INTEGER PRIMARY KEY NOT NULL ,`name` VARCHAR,`name_clean` VARCHAR,`sequence` INTEGER, `display` INTEGER);
推荐答案
我认为您正在遇到此问题:
I think you're running into this:
The keys from input_parameters must match the ones declared in the SQL. Before PHP 5.2.0 this was silently ignored.
参考: PHP:PDOStatement :: execute
我的猜测是PHP尝试匹配两个查询中每个查询的参数.
My guess is that PHP tries to match the parameters on each of the two queries.
您的输入和更新参数看起来相同,所以我认为没有必要拥有这两个集合.尝试将它们折叠成一组
Your input and update parameters look the same, so I don't think there is a requirement to have the two sets. Try collapsing them into one set
[:id] => 1
[:name] => 2
[:name_clean] => 3
[:display] => 1
,并在两个查询中都引用它们.
and referencing them in both queries.
另一个说明:您确定要
WHERE id = 1
这可能也应该是
WHERE id=:id
这篇关于PDO Sqlite一般错误:25绑定或列索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!