Current Location: Home> Function Categories> mysqli::use_result

mysqli::use_result

(mysqli_use_result) Start result set search
Name:mysqli::use_result
Category:MySQLi
Programming Language:php
One-line Description:Starts to retrieve the result set from the last executed query.

Definition and usage

use_result() / mysqli_use_result() function starts to retrieve the result set from the last executed query.

Example

Example 1 - Object-Oriented Style

Start searching the result set from the last executed query:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$sql = "SELECT Lastname FROM Persons ORDER BY LastName;" ;
$sql .= "SELECT Country FROM Customers" ;

// Perform multiple queries
if ( $mysqli -> multi_query ( $sql ) ) {
  do {
    //Storing the first result set
    if ( $result = $mysqli -> use_result ( ) ) {
      while ( $row = $result -> fetch_row ( ) ) {
        printf ( "%s\n" , $row [ 0 ] ) ;
      }
     $result -> close ( ) ;
    }
    // If there are more result sets, print the separator
    if ( $mysqli -> more_results ( ) ) {
      printf ( "--------------\n" ) ;
    }
     // Prepare the next result set
  } while ( $mysqli -> next_result ( ) ) ;
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Start searching the result set from the last executed query:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

$sql = "SELECT Lastname FROM Persons ORDER BY LastName;" ;
$sql .= "SELECT Country FROM Customers" ;

// Perform multiple queries
if ( mysqli_multi_query ( $con , $sql ) ) {
  do {
    //Storing the first result set
    if ( $result = mysqli_use_result ( $con ) ) {
      while ( $row = mysqli_fetch_row ( $result ) ) {
        printf ( "%s\n" , $row [ 0 ] ) ;
      }
      mysqli_free_result ( $result ) ;
    }
    // If there are more result sets, print the separator
    if ( mysqli_more_results ( $con ) ) {
      printf ( "--------------\n" ) ;
    }
     // Prepare the next result set
  } while ( mysqli_next_result ( $con ) ) ;
}

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles