Difference between mysql_fetch_assoc(), mysql_fetch_row(), mysql_fetch_array() functions
- Article
- Comment
Difference between mysql_fetch_assoc(), mysql_fetch_row(), mysql_fetch_array() functions . Certain situations we the PHP programs had a trouble to choose right function. Becasue all the three looks simmilar in nature. We need to understand well before go with live codes. Here i will show you each functions and its working codes. Lets start with mysql_fetch_assoc().
mysql_fetch_assoc() :
Before getting into the working code , here is an common array of data for sample work.
<?php $query=mysql_connect("localhost","root",""); mysql_select_db("kvcodes",$query); ?>
And, now we are going to create a table inside kvcodes database “kv_users“.
CREATE TABLE IF NOT EXISTS `kv_users` ( `id` smallint(6) NOT NULL AUTO_INCREMENT, `user_id` varchar(60) NOT NULL DEFAULT '', `password` varchar(100) NOT NULL DEFAULT '', `real_name` varchar(100) NOT NULL DEFAULT '', `access_id` int(11) NOT NULL DEFAULT '1', `phone` varchar(30) NOT NULL DEFAULT '', `email` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `user_id` (`user_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
And also add some users, than come to data functions,
$query=mysql_query("select * from kv_users"); $row=mysql_fetch_assoc($query); echo $row['id']; echo $row['user_id']; echo $row['password'];
The above function helps you to use the mysql_fetch_assoc functions , which is also similar to mysql_fetch_object().
mysql_fetch_object() :
Here instead of the array, we are using object, Let’s see an example,
$query=mysql_query("select * from kv_users"); $row=mysql_fetch_object($query); echo $row->id; echo $row->user_id; echo $row->password;
mysql_fetch_row() :
This function uses widely for the users table, while retrieving the user login details, this will be more useful,
$query=mysql_query("select * from kv_users"); $row=mysql_fetch_row($query); echo $row[0]; echo $row[1]; echo $row[2];
This will gets each row, and finally,
mysql_fetch_array() :
Here is the sample code for mysql_fetch_array() function.
$query=mysql_query("select * from kv_users"); $row=mysql_fetch_array($query); echo $row['id']; echo $row['user_id']; echo $row['password'];
That’s it for the working codes.