带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064

时间:2016-03-09 07:51:03

标签: php

我知道同一主题存在很多问题,但我无法找到错误。

我创建了一个函数:

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

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

1 个答案:

答案 0 :(得分:1)

无法通过绑定参数来插入您的tablename。您必须将tablename连接到sql中。

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

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