我正在尝试PHP并在准备好的语句中苦苦挣扎。 我有以下代码
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
echo "Connected to $dbname at $host successfully.";
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
$statement = $conn->prepare("SELECT * FROM voicemessages WHERE dir = :dir");
$statement->bindParam("dir", "test");
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
?>
但是收到以下错误:
Connected to voicemail1 at 192.168.1.121 successfully.
Warning: main(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for 'GMT/0.0/no DST' instead in /var/lib/asterisk/agi-bin/KesherVoicemail.php on line 13
Fatal error: Cannot pass parameter 2 by reference in /var/lib/asterisk/agi-bin/MyScript.php on line 13
第13行是bindParam行。
请有人解释有什么问题吗?
答案 0 :(得分:2)
您的错误在于:$statement->bindParam("dir", "test");
应为bindParam(":Placeholder",$variable)
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
echo "Connected to $dbname at $host successfully.";
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
$test = "test";
$statement = $conn->prepare("SELECT * FROM voicemessages WHERE dir = :dir");
$statement->bindParam(":dir", $test);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
?>
答案 1 :(得分:1)
您需要通过参考bindParam
函数传递参数2。
替换此 -
$statement->bindParam("dir", "test");
用这个 -
$test = "test";
$statement->bindParam(":dir", $test);
查看doc
variable:要绑定到SQL语句参数的PHP变量的名称。