MySQL PDO准备语句

I'm trying out PHP and struggling with prepared statements. I've got the following code

<?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);
?>

But getting the following error:

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

Line 13 is the bindParam line.

Please can someone explain what is wrong?

You need to pass parameter 2 by reference to the bindParam function.

Replace this-

$statement->bindParam("dir", "test");

with this-

$test = "test";
$statement->bindParam(":dir", $test);

Check out the doc

variable: Name of the PHP variable to bind to the SQL statement parameter.

Your error is here : $statement->bindParam("dir", "test");

should be 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);
?>