Difference Between Mysqli_fetch_array And Mysqli_fetch_assoc And Mysqli_fetch_row
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
1 2 3 4 5 6 7 8 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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