PHPGurukul

Difference Between Mysqli_fetch_array And Mysqli_fetch_assoc And Mysqli_fetch_row

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
]]>

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


8 comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.