如何在执行PHP / MYSQL预处理语句后获取返回的行?

时间:2016-08-25 20:02:59

标签: php mysql

我知道执行语句的输出是bool。但是,如何得到如下所示的SELECT语句的结果?

<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>

$ sth将为TRUE或FALSE。我感兴趣的是包含名称,颜色等的返回行。

谢谢!

1 个答案:

答案 0 :(得分:0)

$result = $sth->get_result();
while ( $row = $result->fetch_assoc() ) {

    echo $row['name'];

}