准备好的语句在查询时失败

Prepared statement fails on query

所以我遇到了一些尴尬的事情,我真的不明白为什么。当我 运行 原始代码时,我会收到一条错误消息,说我的查询不接受 table。

所以我的代码如下。

$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);  

....

这是我这边的错误还是我不能将参数用作 table?。我宁愿将 table 作为准备好的语句参数加载,但如果没有办法绕过它,我将不得不随心所欲。

无法为 table 名称使用绑定参数。

只能通过绑定占位符提供值。

不能通过绑定占位符提供标识符(table 名称、列名称、函数名称等)。