1

由于某种原因,我无法将 VAR 传递给 PDO SQL 查询。

<?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();
    }
?>
4

2 回答 2

2
$dbh->exec("UPDATE Quiz1 SET `$score` = 1 WHERE Question = 1");

如果出于某种原因必须引用表名或列名,则必须在 MySQL 中使用 `。

于 2012-07-22T09:31:24.257 回答
1

您在$score此处使用变量作为属性名称,并且属性名称不应包含在引号中。尝试这个:

$dbh->exec("UPDATE Quiz1 SET $score = 1 WHERE Question = 1");

(我没有测试过。)

于 2012-07-22T08:07:14.563 回答