Reputation: 55
for some reason I can't pass a VAR into a PDO SQL query .
<?php
$hostname = "host";
$username = "username";
$password = "pass";
$score = "B" ;
try {
$dbh = new PDO("mysql:host=$hostname;dbname='DBNAME'", $username, $password);
echo "Connected to database"; // check for connection
$dbh->exec("UPDATE Quiz1 SET '$score' = 1 WHERE Question = 1"); // THIS DOES NOT
//$dbh->exec("UPDATE Quiz1 SET B = 1 WHERE Question = 1"); // THIS WORKS
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Upvotes: 1
Views: 131
Reputation: 9319
$dbh->exec("UPDATE Quiz1 SET `$score` = 1 WHERE Question = 1");
If you have to quote your table or column names for some reason then you have to use ` in MySQL.
Upvotes: 2
Reputation: 28855
You use the $score
variable here as an attribute name and attribute names should not be in quotes. Try this:
$dbh->exec("UPDATE Quiz1 SET $score = 1 WHERE Question = 1");
(I haven't tested.)
Upvotes: 1