Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • MySQLi functions to fetch records from resultset.(Part-1)

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 630
    Comment on it

    In previous articles we already learned how to connect MySQL database and how to perform different queries using MySQLi. This article demonstrates some MySQLi functions which are used to retrieve row by row data from resultset. The complete article is divided into two parts , this part provides the explanation and implementation details of mysqli_fetch_row() and mysqli_fetch_array() functions in both Procedural and Object oriented style. 

     

    1. mysqli_fetch_row()

    mysqli_fetch_row() fetches a row from the resultset and returns row data as an enumerated array.

     

    Syntax :

     

    a. Procedural Style : mysqli_fetch_row( result )

        result : Specifies mysqli result set.(Required)

     

    b. Object Oriented Style : mysqli_result::fetch_row()

     

     

    Description : mysqli_fetch_row() fetches a row from the resultset and returns the row data as an array with numeric indexes starting from 0. Next subsequent call fetch the next row from result set. The function return NULL if there are no more rows in the resultset.

     

    Result : This function returns an array of strings which corresponds to row data fetched from resultset and it return NULL if there are no more rows in the resultset.

     

    Example : Procedural style

    <?php
    //--------SQL statement---------
    $query = "SELECT * FROM users LIMIT 2";
    
    if ($result = mysqli_query($con, $query))
    {
        echo "<pre>";
        while($res = mysqli_fetch_row($result)){
    	print_r($res);
        }	
        echo "</pre>";
        mysqli_free_result($result);
    }
    ?>

     

    Example : Object Oriented style

    <?php
    //--------SQL statement---------
    $query = "SELECT * FROM users LIMIT 2";
    if ($result = $con->query($query)) 
    {
       echo "<pre>";
       //----fetch rows from resultset----
       while ($res = $result->fetch_row()) {
            print_r($res);
        }
       echo "</pre>";
       $result->free();
    }
    ?>

     

    Output :

    Array
    (
        [0] => 1
        [1] => Amit
        [2] => Kumar
        [3] => amit.kumar21@gmail.com
        [4] => Dehradun
    )
    Array
    (
        [0] => 2
        [1] => Ravi 
        [2] => Singh
        [3] => ravi22singh@yahoo.com
        [4] => Mumbai
    )

     

     

    2. mysqli_fetch_array()

    mysqli_fetch_array() function also used to fetch row from the resultset. It fetches a row from the resultset and return row data as an array of numeric, associative or combination of both types.

     

    Syntax :

     

    a. Procedural Style : mysqli_fetch_array( result, resulttype );

    result : Specifies mysqli result set.(Required)

    resulttype :  Constant value which defines the type of the returned array.(Optional)

     

    resulttype can have one of the following values :

    MYSQLI_NUM : Return a numeric array.

    MYSQLI_ASSOC : Return an associative array.

    MYSQLI_BOTH : Return an array with both numeric and associative keys.

     

    b. Object Oriented Style : mysqli_result::fetch_array ( resulttype )

    resulttype :  Constant value which defines the type of the returned array.

     

    Description : mysqli_fetch_array() fetches a row from the specified resultset result and returns the row data as an array of type specified by passing second optional parameter resulttype. The function returns an array with both numeric and associative keys as the default, if resulttype parameter is not passed. The column names of resultset used as keys for associative array.

     

    The name of fields returned by function are case sensitive. If in a resultset two or more columns have the same name, then the value of the last column overwrites the value of an earlier column with the same name. To get values of multiple columns with the same name it is better to use MYSQLI_NUM type.

     

    Result : This function returns an array of row data fetched from the resultset. It returns NULL, if there are no more rows in resultset.

     

    Example : Procedural Style

    <?php
    //--------SQL statement---------
    $query = "SELECT * FROM users LIMIT 3";
    
    if ($result = mysqli_query($con, $query))
    {
        echo "<pre>";
        echo "Using MYSQLI_NUM : <br>";
        $res = mysqli_fetch_array($result,MYSQLI_NUM);
        print_r($res);
        echo "Using MYSQLI_ASSOC : <br>";	
        $res1 = mysqli_fetch_array($result,MYSQLI_ASSOC);
        print_r($res1);
        echo "Using MYSQLI_BOTH : <br>";	
        $res = mysqli_fetch_array($result,MYSQLI_BOTH);
        print_r($res);		
        echo "</pre>";
        mysqli_free_result($result);
    }
    ?>

     

    Example : Object Oriented Style

    <?php
    //--------SQL statement---------
    $query = "SELECT * FROM users LIMIT 3";
    if ($result = $con->query($query)) 
    {
       echo "<pre>";
       echo "Using MYSQLI_NUM : <br>";
       $res = $result->fetch_array(MYSQLI_NUM);
       print_r($res);
       echo "Using MYSQLI_ASSOC : <br>";	
       $res1 = $result->fetch_array(MYSQLI_ASSOC);
       print_r($res1);
       echo "Using MYSQLI_BOTH : <br>";	
       $res = $result->fetch_array(MYSQLI_BOTH);
       print_r($res);		
       echo "</pre>";
       $result->free();
    }
    ?>

     

    Output :

    Using MYSQLI_NUM :
    Array
    (
        [0] => 1
        [1] => Amit
        [2] => Kumar
        [3] => amit.kumar21@gmail.com
        [4] => Dehradun
    )
    Using MYSQLI_ASSOC :
    Array
    (
        [id] => 2
        [first_name] => Ravi 
        [last_name] => Singh
        [email] => ravi22singh@yahoo.com
        [city] => Mumbai
    )
    Using MYSQLI_BOTH :
    Array
    (
        [0] => 3
        [id] => 3
        [1] => Deepak
        [first_name] => Deepak
        [2] => Rawat
        [last_name] => Rawat
        [3] => deepak.rawat@gmail.com
        [email] => deepak.rawat@gmail.com
        [4] => New Delhi
        [city] => New Delhi
    )

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: