未捕获的异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突:1064


Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064

我知道同一主题有很多问题,但我就是找不到我的错误。

我创建了一个函数:

function getLicenseType($computerid){
    $stmt = $GLOBALS['pdo'] -> prepare("SELECT * FROM :tabelle WHERE computerid = :id");
    $stmt->bindParam("id",$computerid);
    $stmt->bindParam("tabelle",$GLOBALS['licensetable']);

    $stmt->execute();
    $licenseinfo = $stmt->fetch();
    if ($licenseinfo != false )
    {
        return $licenseinfo['licensetype'];
    }
    else
    {
        return "NoLicense";
    }
}

错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 ''backuplicenses' WHERE computerid = '40'' at line 1' in api.php:42 Stack trace: #0 api.php(42): PDOStatement->execute() #1 license.php(10): getLicenseType('40') #2 {main} thrown in api.php on line 42

我检查了所有变量,但它们不为空。所以这不可能是问题。

不能通过绑定参数来插入表名。您必须将表名连接到 sql 中。

$sql = "SELECT * FROM ". $GLOBALS['licensetable'] ." WHERE computerid = :id";

但是,请检查有效性$GLOBALS['licensetable']