PDO mysql选择查询不执行

时间:2014-11-13 12:54:52

标签: php mysql pdo

为什么这个PDO没有执行?

$statement = $db->prepare("select * from quest where id = :'"+$testno+"'");
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row["questions"];

" Q"没有回应。

3 个答案:

答案 0 :(得分:4)

您应该将命名占位符放在准备好的sql中,并在以后将其绑定到所需的值:

$statement = $db->prepare("select * from quest where id = :id");
$statement->bindParam(':id', $testno, \PDO::PARAM_INT);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row["questions"];

假设id为整数,如果不是 - 将\PDO::PARAM_INT替换为\PDO::PARAM_STR

答案 1 :(得分:1)

$statement = $db->prepare("select * from quest where id = :testno");
$statement->execute(array(':testno'=>$testno));
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row["questions"];

答案 2 :(得分:0)

@ X10nD,只需使用如下代码......:)

$query = "SELECT * FROM quest WHERE id = ?";
$statement = $db->prepare($query);
$statement->bindParam(1,$testno);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row['questions'];