Having trouble using bindParam with PDO/mysql in PHP.
Here is my simplified code, which does still produce the error:
$tableID = "bmb_job";
$columnName = "job_id";
$recordID = 1;
$stmt = $dbh->prepare('SELECT * FROM ? WHERE ? = ? LIMIT 1');
$stmt->bindParam(1, $tableID);
$stmt->bindParam(2, $columnName);
$stmt->bindParam(3, $recordID, PDO::PARAM_INT);
$stmt->execute();
$jobArray = $stmt->fetch();
And 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 ''bmb_job' WHERE 'job_id' = 1 LIMIT 1' at line 1' in /home/danny/public_html/bmbbooking/ajax/getuser.php:21 Stack trace: #0 /home/danny/public_html/bmbbooking/ajax/getuser.php(21): PDOStatement->execute() #1 {main} thrown in /home/danny/public_html/bmbbooking/ajax/getuser.php on line 21
Initially I thought it was related to it thinking $recordID was a string, but as far as I can tell MySQL doesn't like ' in its query, only backticks... I tested this out by running the query in phpMyAdmin, the query fails with 'bmb_job' WHERE 'job_id' = 1 LIMIT 1
but works with backticks
It did work before when I did $stmt = $dbh->prepare('SELECT * FROM $tableID WHERE $columnName = $recordID LIMIT 1');
, but I want to use bindParam if I can.