解析错误:语法错误,意外的'$ _GET'(T_VARIABLE)

时间:2015-10-11 09:49:23

标签: php mysql sql pdo get

当我尝试从URL加载“id”时,它似乎无法正常工作。有什么理由吗?感谢您的帮助!

<?php

class Poll{

    private $db;
    private $presidentid = $_GET['id'];

    public function __construct($db){
        $this->db = $db;
    }

    public function getPollData(){
        $sql = "SELECT poll_question, yes, no FROM poll WHERE poll_id = $presidentid";
        $statement = $this->db->prepare($sql);
        $statement->execute();
        $pollData = $statement->fetchObject();
        return $pollData;
    }

}

?>

2 个答案:

答案 0 :(得分:1)

我们不会将变量放在准备好的查询中。

$sql = "SELECT poll_question, yes, no FROM poll WHERE poll_id = :id";

$statement = $this->db->prepare($sql);
$statement->bindParam(':id', $this->presidentid);
$statement->execute();

请参阅:http://php.net/manual/fr/pdo.prepare.php

答案 1 :(得分:0)

private $presidentid = $_GET['id'];

用波纹管替换上面的线,它会正常工作,

class Poll{

    private $db;
    private $presidentid;
    public function __construct($db){
            $this->db = $db;
            $this->presidentid = $_GET['id'];
        }

我会解释,为什么你不应该使用像私人$ presidentid = $ _GET [&#39; id&#39;]; 一旦你的问题得到解决。