PHP PDO - 何时使用bindParam(1, $blabla) 与bindParam(':blabla', $blabla)
总之,
我不太理解命名占位符 bindParam(':blabla', $blabla)
与编号占位符 bindParam(1, $blabla)
的用法。这主要是可读性的问题吗?
谢谢,
杰拉格
All,
I don't really understand the usage case for named placeholders bindParam(':blabla', $blabla)
versus numbered placeholders bindParam(1, $blabla)
. Is it mostly a question of readability?
Thanks,
JDelage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您想要通过在中间添加一个额外的占位符来扩展查询时,编号占位符将被证明是 PITA,要求您在占位符之后使用占位符的所有地方重新编号刚刚插入。
另一方面,命名占位符不会出现此问题,因为占位符的位置对于占位符的绑定并不重要。
numbered placeholders will prove to be a PITA when you want to expand your query by adding an additional placeholder in the middle, requiring you to renumber everywhere you use placeholders after the placeholder you just inserted.
Named placeholders, on the other hand, won't have this problem, as the position of the placeholder does not matter for the binding of the placeholder.
这主要只是一个可读性的问题。就我个人而言,如果可能的话,我会使用命名占位符。我通常只会在构建动态查询时使用编号占位符,在该动态查询中您在运行时之前不知道到底有什么参数或有多少参数。
It's mostly just a readability thing. Personally, I would used named placeholders when possible. I would generally use numbered placeholders only if I was building a dynamic query where you don't know exactly what or how many parameters there will be until runtime.
简单答案:
Simple Answers: