繁体   English   中英

PDO查询,变量和字符串串联为表名

[英]PDO query with variable and string concatenated as table name

我有这个查询

$prefix = 'de_';
    $sql = "INSERT INTO $prefix.request_products (created_at, request_id) VALUES (NOW(), :request_id)";

哪个de_request_products ,但是php或PDO(或两者)正在查询中写入点,表名称为de_request_products但查询构建为de_.request_products

如果我只是在$ prefix和字符串之间使用空格,则PDO会引发错误。

如何在查询中将字符串和变量连接起来还有其他可能性,还是这样构建它的唯一方法:

$prefix = 'de_';
$table = 'request_products';
$concat = $prefix.$table;
$sql = INSERT INTO $concat ...

使用${variable}语法:

$sql = "INSERT INTO ${prefix}request_products (created_at, request_id) VALUES (NOW(), :request_id)";

手册在这里: http : //php.net/manual/pl/language.types.string.php说:“将变量名括在花括号中以明确指定名称的结尾。” 因此,这应该使您感到高兴:

$prefix = 'de_';
$sql = "INSERT INTO ${prefix}request_products (created_at, request_id) VALUES (NOW(), :request_id)";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM