Difference Between Mysql_fetch_array And Mysql_fetch_assoc And Mysql_fetch_row

1955 0

$1*/ mo hosting! Get going with GoDaddy!


PHP Beginners get confused with mysql_fetch_row(), mysql_fetch_object(), mysql_fetch_assoc(), mysql_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

 

Mysql_fetch_row()

Mysql_fetch_row fetch result row an numeric way. This function return a row where the valuew 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  mysql_fetch_row() function , if you are fetching the selected records then their order matterswhile 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

Mysql_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

Mysql_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 mysql_fetch_row and mysql_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

Mysql_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

In this article

Join the Conversation


Share