我想执行以下查询:
选择*,(SELECT COUNT(*) FROM `tab2` WHERE `parent` = :id) AS `sum`从`tab1`哪里`id` = :id
如您所见,:id 占位符在查询中出现了两次.因此,如果我尝试使用以下命令执行此语句:
$q->execute(['id'=>$row_id]);我收到错误:
致命错误:未捕获的异常PDOException",消息为SQLSTATE[HY093]:参数编号无效所以我必须重写准备好的查询并使用 :id1 和 :id2 占位符执行数组,这对我来说看起来有点愚蠢.
这是在准备好的语句的多个地方使用一个占位符的唯一方法吗?
PDO::prepare 表示
<块引用>[y]你不能在准备好的语句中多次使用同名的命名参数标记,除非模拟模式打开.
由于通常最好关闭仿真模式(因此数据库执行准备好的语句),因此您必须使用 id_0、id_1 等>
I'd like to perform the following query:
SELECT
*,
(SELECT COUNT(*) FROM `tab2` WHERE `parent` = :id) AS `sum`
FROM `tab1`
WHERE `id` = :id
As you can see :id placeholder appeared twice in the query. So if I'd try to execute this statement with:
$q->execute(['id'=>$row_id]);
I'm receiving the error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number
So I have to rewrite the prepared query and execute array with :id1 and :id2 placeholders which looks a bit stupid for me.
Is it the only way to use one placeholder in several places of the prepared statement?
PDO::prepare states that
[y]ou cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.
Since it's generally better to leave emulation mode off (so the database does the prepared statement), you'll have to use id_0, id_1, etc.
这篇关于PHP 的 PDO 准备语句:我可以多次使用一个占位符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
MySQLi准备好的语句&foreach 循环MySQLi prepared statement amp; foreach loop(MySQLi准备好的语句amp;foreach 循环)
mysqli_insert_id() 是从整个服务器还是从同一用户获Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是从整个服务器还是从同一用户获取记录?)
PHP MySQLi 无法识别登录信息PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 无法识别登录信息)
mysqli_select_db() 需要 2 个参数mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 个参数)
Php mysql pdo 查询:用查询结果填充变量Php mysql pdo query: fill up variable with query result(Php mysql pdo 查询:用查询结果填充变量)
MySQLI 28000/1045 用户“root"@“localhost"的访问MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用户“root@“localhost的访问被拒绝)