Difference Between Mysqli_fetch_array And Mysqli_fetch_assoc And Mysqli_fetch_row

7

- Advertisement -

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

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.

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.

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.

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.

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

- Advertisement -

Leave A Reply

Your email address will not be published.

7 Comments
  1. Interview Sortout says

    beautifully explained the differences.

Leave A Reply

Your email address will not be published.