Don't Afraid of Source Code

Difference Between Mysqli_fetch_array And Mysqli_fetch_assoc And Mysqli_fetch_row

7

PHP Beginners get confused with mysqli_fetch_row(), mysqli_fetch_object(), mysqli_fetch_assoc(), mysqli_fetch_array() functions.Basically all of these functions performs with a similar process.
Create a Table User with 5 fields id,name,email,contactno,addrss,posting_date
Structure of User table

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `contactno` bigint(11) NOT NULL,
  `addrss` longtext NOT NULL,
  `posting_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Mysqli_fetch_row()

Mysqli_fetch_row fetch result row an numeric way. This function return a row where the value will come in the order as they are defined in the query, and the keys will span from 0 to one less than the number of columns selected.

<?php
$con = mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"select * from user");
while ($rows = mysqli_fetch_row($result))
{
    echo $rows[0];
    echo "<br>";
    echo $rows[1];
    echo "<br>";
    echo $rows[2];
    echo "<br>";
     echo $rows[3];
    echo "<br>";
     echo $rows[4];
    echo "<br>";
     echo $rows[5];
    echo "<br>";
}
?>

Output
2
Anuj
anuj.lpu1@gmail.com
8285703354
New Delhi
2016-03-08 11:06:18
3
Rahul
rahul@gmail.com
2147483647
test address
2016-03-11 10:11:48

Here with this  mysqli_fetch_row() function , if you are fetching the selected records then their order matters while getting the result.
For example if you have written, “select id, name,email from user”
then,
$rows[0]; will have the value of id
$rows[1]; will have the value of name
$rows[2]; will have the value of email
but if you write ,”select id, email,name from user”
then,
$rows[0]; will have the value of id
$rows[1]; will have the value of email
$rows[2]; will have the value of username

Mysqli_fetch_assoc()

Fetch a result row as an associative array.This function will return a row as an associative array where the column names will be the keys storing corresponding value.

<?php
$con = mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"select  * from user");
while ($rows = mysqli_fetch_assoc($result))
{
    echo $rows['id'];
    echo "<br>";
    echo $rows['name'];
    echo "<br>";
    echo $rows['email'];
    echo "<br>";
    echo $rows['contactno'];
    echo "<br>";
     echo $rows['addrss'];
    echo "<br>";
 echo $rows['posting_date'];
    echo "<br>";
}
?>

Output
2
Anuj
anuj.lpu1@gmail.com
8285703354
New Delhi
2016-03-08 11:06:18
3
Rahul
rahul@gmail.com
2147483647
test address
2016-03-11 10:11:48

Mysqli_fetch_array()

Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array.This function will actually return an array with both the contents of mysqil_fetch_row and mysqli_fetch_assoc merged into one. It will both have numeric and string keys.

<?php
$con = mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"select  * from user");
while ($rows = mysqli_fetch_arrayc($result))
{
    echo $rows['id'];
    echo "<br>";
    echo $rows['name'];
    echo "<br>";
    echo $rows['email'];
    echo "<br>";
    echo $rows['contactno'];
    echo "<br>";
     echo $rows['addrss'];
    echo "<br>";
 echo $rows['posting_date'];
    echo "<br>";
/* Now here both associative array and numeric array will work. */
echo $rows[0];
 echo "<br>";
 echo $rows[1];
 echo "<br>";
 echo $rows[2];
 echo "<br>";
 echo $rows[3];
 echo "<br>";
 echo $rows[4];
 echo "<br>";
 echo $rows[5];
 echo "<br>";
}
?>

Output
2
Anuj
anuj.lpu1@gmail.com
8285703354
New Delhi
2016-03-08 11:06:18
3
Rahul
rahul@gmail.com
2147483647
test address
2016-03-11 10:11:48

Mysqli_fetch_object()

Fetch a result row as an object.

<?php
$con = mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"select  * from user");
while ($rows = mysqli_fetch_object($result))
{
    echo $rows->id;
    echo "<br>";
    echo $rows->name;
    echo "<br>";
    echo $rows->email;
    echo "<br>";
    echo $rows->contactno;
    echo "<br>";
     echo $rows->addrss;
    echo "<br>";
 echo $rows->posting_date;
    echo "<br>";
}
?>

Output
2
Anuj
anuj.lpu1@gmail.com
8285703354
New Delhi
2016-03-08 11:06:18
3
Rahul
rahul@gmail.com
2147483647
test address
2016-03-11 10:11:48

Leave A Reply

Your email address will not be published.

7 Comments
  1. Interview Sortout says

    beautifully explained the differences.

  2. MehulKumar Poraniya says

    good information .explain in easy way

  3. Shafi says

    Good Explanation, Keep it up, I appreciate.

  4. Anuj kumar says

    issue is fixed now.

  5. Israr Zaib says

    Anuj kumar, In last part of your article you have use mysql_fetch_assoc() instead of mysql_fetch_object()
    so kindly correct it.
    thanks

    1. Anuj kumar says

      issue is fixed now.

  6. Rajesh Polshetwar says

    nice explain a concept

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy