PHP/PDO:创建表时,准备好的语句不起作用? [英] PHP/PDO: Prepared statements don't work when creating a table?

查看:54
本文介绍了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!

我最终需要在准备好的语句的TOP上执行sprintf,以添加预定义的表名.

I end up needing to do a sprintf on TOP of the prepared statement to add in a predefined table name.

我到底在这里想念什么?

What on earth am I missing here?

推荐答案

我在手册中找不到任何清晰的内容,但是在用户贡献的注释中,参数的使用旨在用于实际的仅,而不是表名,字段名等.

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:创建表时,准备好的语句不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆