Hi,
I want to know the difference between mysql_fetch_assoc , mysql_fetch_array & mysql_fetch_object. I am novice and learning php & mysql. Thanks in advance…
Please Clarify with an example…
-Naim
http://us.php.net/manual/en/ref.mysql.php
These are very basic questions that can be found on the php website
$rows = mysql_fetch_array( “select name, address from people”);
Means you then get each of the row results as a straight array, meaning you dont necessarily need to know in advance the order elements arrive via the select.
foreach( $rows as $row )
echo $row[0] . ’ lives at ’ . $row[1] ;
$rows = mysql_fetch_assoc( “select name, address from people” );
Means you then get each of the row results as a named array elements, an associative array.
foreach( $rows as $row )
echo $row[‘name’] . ’ lives at ’ . $row[‘address’];
Which is now dependent on you knowing what the result columns are named.
$rows = mysql_fetch_object( “select name, address from people” );
Means you then get each of the row results using the object notation.
foreach( $rows as $row )
echo $row->name . ’ lives at ’ . $row->address ;
Which again relies upon your knowing what is coming out of the database, and in my opinion is better because a) its easier to read, and b) if you don’t use OOP, it at least gets you thinking about and using OO notation.
The manual on [fphp]mysql_fetch_object()[/fphp] leads to all the others, and provides definitive descriptions.