PDO 中附加到字符串的问号
Question mark attached to string in PDO
我有一个看起来像这样的字符串:
use Illuminate\Database\Capsule\Manager as DB;
DB::select("SELECT id FROM table_?", [$var]);
所以查询看起来像 SELECT id FROM table_users
。
问题是我遇到语法错误:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1
为什么 eloquent(或一般的 PDO)不会替换附加到字符串的 ?
?
PDO 不只是取代 ?与其他字符串。它执行查询的语义解析,构建执行计划,然后使用您绑定到它的值执行它。没有 table 名称,您无法构建执行计划。因此,您不能像那样绑定属于 table 名称的任意字符串。您将不得不求助于字符串连接:
DB::select("SELECT id FROM table_" . "users");
或者,由于您已经将所有字符串作为文字,所以您自己来做:
DB::select("SELECT id FROM table_users");
我有一个看起来像这样的字符串:
use Illuminate\Database\Capsule\Manager as DB;
DB::select("SELECT id FROM table_?", [$var]);
所以查询看起来像 SELECT id FROM table_users
。
问题是我遇到语法错误:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1
为什么 eloquent(或一般的 PDO)不会替换附加到字符串的 ?
?
PDO 不只是取代 ?与其他字符串。它执行查询的语义解析,构建执行计划,然后使用您绑定到它的值执行它。没有 table 名称,您无法构建执行计划。因此,您不能像那样绑定属于 table 名称的任意字符串。您将不得不求助于字符串连接:
DB::select("SELECT id FROM table_" . "users");
或者,由于您已经将所有字符串作为文字,所以您自己来做:
DB::select("SELECT id FROM table_users");