Ali M Ali M - 3 months ago 8
MySQL Question

How to get the returned rows after executing a PHP/MYSQL prepared statement?

I know the output of an execute statement is a bool. But then how do you get the result of a SELECT statement like below?

<?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 will be either TRUE or FALSE. What I'm interested in is the return rows containing the name, colour and etc.

Thanks!

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

    echo $row['name'];

}