gpt4 book ai didi

php - PDO准备查询错误: number of bound variables does not match number of tokens

转载 作者:行者123 更新时间:2023-11-29 12:22:06 26 4
gpt4 key购买 nike

我正在将其他人编写的一些未准备的 PDO 查询转换为准备好的 PDO 查询。所有 undefined variable 和 POST 变量实际上都在此例程中执行;为了简洁起见,我省略了它们的定义。证据就在布丁中,对于此处提供的现有的、未准备的查询,有效:

$query = sprintf('INSERT INTO galleries (title, description, meta_description, published) VALUES ("%s", "%s", "%s", %d)', addslashes($_POST['gallery_name']), addslashes($_POST['gallery_description']), addslashes($_POST['gallery_meta_description']), intval($published));
$connection->query($query);

但是,我将此代码转换为完全准备好的查询,但没有:

$query = 'INSERT INTO galleries (title, description, meta_description, published) VALUES (":title", ":description", ":meta_description", :published)';

$PdoStatementObject = $connection->prepare($query);

$title = addslashes($_POST['gallery_name']);
$description = addslashes($_POST['gallery_description']);
$meta_description = addslashes($_POST['gallery_meta_description']);
$published_int = intval($published);

$PdoStatementObject->bindValue(":title", $title, PDO::PARAM_STR);
$PdoStatementObject->bindValue(":description", $description, PDO::PARAM_STR);
$PdoStatementObject->bindValue(":meta_description", $meta_description, PDO::PARAM_STR);
$PdoStatementObject->bindValue(":published", $published_int, PDO::PARAM_INT);

$PdoStatementObject->execute();

生成的错误消息是:

PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

我做错了什么?在我看来,好像每个参数都被考虑在内;是什么导致解释器声称存在 token 计数不匹配?

最佳答案

去掉占位符中的双引号

(":title", ":description", ":meta_description", :published)

(:title, :description, :meta_description, :published)

查看文档http://php.net/manual/en/pdo.prepared-statements.php

关于php - PDO准备查询错误: number of bound variables does not match number of tokens,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28849842/

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