所以我遇到了一些尴尬的事情,我无法弄明白为什么。当我运行原始代码时,我会收到一条错误消息,说我的查询是不可接受的。
所以我的代码如下。
$parts = array(
"engine",
"reactor"
);
$count = count($parts);
for($x = 0; $x < $count; $x++) {
$table = 'ship_'.$parts[$x];
$sql = "SELECT * FROM ? WHERE UserId = ?";
$stmt1 = mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($stmt1,'si',$table, $n_userid)
.....
所以这会导致错误
Fatal error: Wrong SQL: SELECT * FROM ? WHERE UserId = ? Error: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? WHERE UserId = ?'
但是,当我执行以下操作时,它运行正常。
for($x = 0; $x < $count; $x++) {
$table = 'ship_'.$parts[$x];
$sql = "SELECT * FROM ". $table ." WHERE UserId = ?";
$stmt1 = mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($stmt1,'i', $n_userid);
....
这是我身边的错误还是我不能使用参数作为表?我更希望将表作为预处理语句参数加载,但如果没有办法,我将不得不使用我得到的东西。
答案 0 :(得分:1)
不能对表名使用bind参数。
只能通过绑定占位符提供值。
不能通过绑定占位符提供标识符(表名,列名,函数名等)。