I have a query that looks like this:
SELECT number
FROM table1
INNER JOIN table2
WHERE name = 'stack_overflow' AND table1.id = table2.id
AND user_id = 5
This returns a number. It does the right thing, but when inside name I pass a name that does not exist in db, PHP gives me an error. This is how I am executing it:
$stmt = $this->db->prepare($sql);
$stmt->execute();
$x = $stmt->fetchColumn();
I always get the correct $x value when the name exists in the table, however when it doesn’t, I get the following 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 ‘name_that_is_not_in_table’
No idea….
Try to pass name in this way: