Get the current row of a result set, then print each field's value in PHP

Description

The following code shows how to get the current row of a result set, then print each field's value.

Example


// ww  w .j  a va  2s  . co m
<?php
     $con=mysqli_connect("localhost","root","","test");
     // Check connection
     if (mysqli_connect_errno()){
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }

     $sql="SELECT name,birthday FROM employee";

     if ($result=mysqli_query($con,$sql)){
         while ($obj=mysqli_fetch_object($result)){
             printf("%s (%s)\n",$obj->Lastname,$obj->Age);
         }
         // Free result set
         mysqli_free_result($result);
      }

      mysqli_close($con);
?>




















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented