Skip to content

PHP Function to get data from MySQL Database?

Data retrieving is vital in Web Applications, so in this post i am going to write about Data Retrieving function in PHP From MySQL Database. There are several options are available to fetch data from MySQL Database Using PHP. However i am gonna write about most frequently used methods.

Functions List :

mysql_fetch_arrray() : This function returns row as an associative array ,with numeric index.
mysql_fetch_object() : Returns an object with properties that correspond to the returned row.
mysql_fetch_assoc() : This function returns results as an associative array.
mysql_fetch_row() : Get a result row as an enumerated array

Note : functions will return FALSE, if there is no rows

Syntax Of The Functions :


//$resource : Required. The $resource is come from the result of the mysql_query() function


mysql_fetch_array ($resource, $array_type);
//Fetch a result row as an associative array, a numeric array, or both
//$array_type : Optional. here $array_type is type of array to be fetched, It is a constant it can take the following values MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH, MYSQL_BOTH is the default second optional parameter

mysql_fetch_assoc($resource);
//Fetch a result row as an associative array

mysql_fetch_object($resource, $class_name, $params);
//Fetch a result row as an object
//$class_name : Required, The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned.
//$params : Optional, array of parameters to pass to the constructor for class_name objects. 


mysql_fetch_row($resource);
//Get a result row as an enumerated array

Which one is Faster, Best Performance?

Performance wise mysql_fetch_assoc() is better , when we use mysql_fetch_array we get a duplicate set of data consisting of both numbered index as well as associative arrays which of-course is our performance blocker.So when ever you are using mysql_fetch_array function always specify the return_type of the result set array needed.mysql_fetch_object ,mysql_fetch_object performance is justified as it returns objects instead of native arrays which will always bring in a better memory usages than any other kinds of output array.

How to use function ? PHP Example Script

';
	echo 'Contact Name = '.$resource['contact_name'].'
'; echo 'Contact Number = '.$resource['contact_number'].'
'; echo '

'; } */ // Using mysql_fetch_object() /*while($row = mysql_fetch_object($resource)) { //print_r($resource); echo 'Contact ID = '.$resource->contact_id.'
'; echo 'Contact Name = '.$resource->contact_name.'
'; echo 'Contact Number = '.$resource->contact_number.'
'; echo '

'; } */ // Using mysql_fetch_array() while($row = mysql_fetch_array($resource)) { //print_r($resource); echo 'Contact ID = '.$resource['contact_id'].'
'; echo 'Contact Name = '.$resource['contact_name'].'
'; echo 'Contact Number = '.$resource['contact_number'].'
'; echo '

'; } // or while($row = mysql_fetch_array($resource)) { //print_r($resource); echo 'Contact ID = '.$resource[0].'
'; echo 'Contact Name = '.$resource[1].'
'; echo 'Contact Number = '.$resource[2].'
'; echo '

'; } echo "Fetched data successfullyn"; mysql_close($conn); ?>
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments