Handling Results

PHP and MySQL
PHP and MySQL : Home
Introduction to MySQL
mysqli class
Creating Connection
Performing Query
Handling Results

Results
mysqli::query() function can return both boolean and object of class mysqli_result. FALSE boolean means that query is not executed i.e. error occured. While using SELECT statement you will be given object of class mysqli_result. You can understand that with this example:

<?php
$mysqli_object = new mysqli("localhost","","","db");
$sql1 = "SELECT * FROM data WHERE address = 'delhi'";
$sql2 = "CREATE TABLE tb(va INT)";

$result2 = $mysqli_object->query($sql2);
$result1 = $mysqli_object->query($sql1);
// Printing details about both results
echo var_dump($result1);
echo "<br><br>";
echo var_dump($result2);

?>


Output
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(5) ["lengths"]=> NULL ["num_rows"]=> int(6) ["type"]=> int(0) }

bool(false)

You can see the $result1 is an object of class mysqli_result. The motive of this tutorial is to get the data out of this mysqli_result class's object. To handle mysqli_result
mysqli_result
Some useful properties and methods-
Properties:

Methods

<?php
$mysqli_object = new mysqli("localhost","","","db");
$sql = "SELECT * FROM data WHERE address = 'delhi'";

$result = $mysqli_object->query($sql);
// Using loop to get all the rows one at a time
while($row=$result->fetch_assoc()){

    // As we don't know about array we use foreach loop
    // To get all the elements of array with keys
    foreach ($row as $key => $value) {
        echo $key."=>".$value." &nbsp;";
    }
    echo "<br>";
}

?>


Output
srno=>1  name=>Rohit  age=>23  address=>delhi  
srno=>2  name=>Pranav  age=>20  address=>delhi  
srno=>4  name=>Sumit  age=>22  address=>delhi  
srno=>6  name=>Rahul  age=>31  address=>delhi  

fetch_assoc fetches one row at a time so we used while loop. This loop will continue as long as fetch_assoc gives a row(array). So we are getting all the results using this loop.

Here we don't know about the values and keys of the given array . So we will use foreach loop to print all the data. This may not be the case everytime. If we knew about the column names then we could have used $row['columnname'] as $row is an array with indices column name of the result table.
Handling Results (Procedural Method)

<?php
$connect = mysqli_connect("localhost","","","db");
$sql = "SELECT * FROM data WHERE address = 'delhi'";

$result = mysqli_query($connect,$sql);
// Using loop to get all the rows one at a time
while($row=mysqli_fetch_assoc($result)){
    // As we don't know about array we use foreach loop
    // To get all the elements of array with keys
    foreach ($row as $key => $value) {
        echo $key."=>".$value." &nbsp;";
    }
    echo "<br>";
}

?>


Output
srno=>1  name=>Rohit  age=>23  address=>delhi  
srno=>2  name=>Pranav  age=>20  address=>delhi  
srno=>4  name=>Sumit  age=>22  address=>delhi  
srno=>6  name=>Rahul  age=>31  address=>delhi  

Login to Track Your Progress