问题描述
我想执行以下查询:
选择*,(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设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)