PHP/PDO: Prepared statements don#39;t work when creating a table?(PHP/PDO:创建表时准备好的语句不起作用?)
问题描述
当我使用 PDO 准备好的语句,并使用它插入表名到查询失败时,一个简单的例子:
When I am using a PDO prepared statement, and use it to plug in a table name to the query it fails, a quick example:
$stmt = $dbh->prepare("CREATE TABLE ? (id foo, int bar,...)");
$stmt->execute(Array('table_foobar'));
它所做的只是将 ?
替换为 'table_foobar'
,单引号不允许为我创建表!
All it does is replaces ?
with 'table_foobar'
, the single quotes don't allow creation of the table for me!
我最终需要在准备好的语句的顶部执行 sprintf
以添加预定义的表名.
I end up needing to do a sprintf
on TOP of the prepared statement to add in a predefined table name.
我到底错过了什么?
推荐答案
我在手册中找不到任何明确的内容,但是查看用户贡献的注释,参数的使用是针对实际值仅,不包括表名、字段名等
I can find nothing clear in the manual, but looking at the User Contributed Notes, the use of parameters is intended for actual values only, not table names, field names etc.
应该(并且可以)使用正常的字符串连接.
Normal string concatenation should (and can) be used.
$tablename = "tablename";
$stmt = $dbh->prepare("CREATE TABLE `$tablename` (id foo, int bar,...)");
这篇关于PHP/PDO:创建表时准备好的语句不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP/PDO:创建表时准备好的语句不起作用?


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01