Php MySQL PDO准备的语句

Php MySQL PDO准备的语句,php,mysql,pdo,Php,Mysql,Pdo,我正在试用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("Cou

我正在试用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);
?>
第13行是bindParam行


有人能解释一下怎么回事吗?

您需要通过引用
bindParam
函数来传递参数2

替换这个-

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

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

变量:要绑定到SQL语句参数的PHP变量的名称


您需要通过引用
bindParam
函数来传递参数2

替换这个-

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

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

变量:要绑定到SQL语句参数的PHP变量的名称


您的错误在这里:
$statement->bindParam(“dir”、“test”)

应该是
bindParam(“:占位符“,$variable)



您的错误在这里:
$statement->bindParam(“dir”,“test”)

应该是
bindParam(“:占位符“,$variable)



你的答案仍然不正确。。错过了上校。谢谢你的检查!你的回答仍然不正确。。错过了上校。谢谢你的检查!建议您将所有查询(
SELECT*
)放入它们自己的类文件中。建议您将所有查询(
SELECT*
)放入它们自己的类文件中。