1

I know there have been a lot of question with the same topic, but i am just not able to find my mistake.

I create a function:

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";
    }
}

The error:

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

I checked all variables but they are not null. So that can´t be the problem.

IchBestäube
  • 61
  • 3
  • 11
  • where is `$GLOBALS` defined, if these are class properties try `$this->GLOBALS['licensetype']` else at top of function make variable global as `global $GLOBAL` – alamnaryab Mar 09 '16 at 07:54

1 Answers1

1

Your tablename cannot be inserted by binding a parameter. You have to concatenate the tablename into the sql.

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

However, check the $GLOBALS['licensetable'] on validity.

low_rents
  • 4,481
  • 3
  • 27
  • 55
Perrykipkerrie
  • 380
  • 1
  • 3
  • 12