Ali M Ali M - 1 year ago 128
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?

/* 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.


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

    echo $row['name'];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download