Iterate through the result set in PHP

Description

The following code shows how to iterate through the result set.

Example


// w w  w  .  java 2  s. c o m
<?php

    $mysqli = new mysqli('localhost', 'rootuser', '', 'test');

    $query = 'SELECT id,  name,birthday FROM employee';
    $mysqli->query($query);

    $result = $mysqli->query($query, MYSQLI_STORE_RESULT);

    // Iterate through the result set
    while(list($sku, $name, $price) = $result->fetch_row())
        printf("(%s) %s: \$%s <br />", $sku, $name, $price);

    // Recuperate the query resources
    $result->free();

    // Perhaps perform some other large query

?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented