gpt4 book ai didi

php - 解析错误 : syntax error, 意外 '$_GET' (T_VARIABLE)

转载 作者:行者123 更新时间:2023-11-29 11:56:49 24 4
gpt4 key购买 nike

当我尝试从 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;
}

}

?>

最佳答案

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

$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

关于php - 解析错误 : syntax error, 意外 '$_GET' (T_VARIABLE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com