我在PDO上阅读并在StackOverFlow上搜索了关于pdo和prepare语句的内容.我想知道什么是好处或使用准备声明.例如:
$sql = 'SELECT name,colour,calories FROM fruit WHERE calories < :calories AND colour = :colour'; $sth = $dbh->prepare($sql,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $sth->execute(array(':calories' => 150,':colour' => 'red')); $red = $sth->fetchAll();
VS
$sql = "SELECT name,calories FROM fruit WHERE calories < $calories AND colour = $colour"; $result = $connection->query($query); $row = $result->fetch(PDO::FETCH_ASSOC);
两个查询都将返回相同的结果,所以为什么使用准备,对我来说看起来它会慢,因为你必须执行额外的步骤.
谢谢