5 PHP Functions to Fetch Data from Database

By | September 1, 2012

Mostly used php functions for fetching data from database are given. I have listed 5 php functions to fetching data.

  1. mysql_fetch_assoc()
  2. mysql_fetch_object()
  3. mysql_fetch_array()
  4. mysql_fetch_row()
  5. mysql_result()
Database
Single field only available in this database and field’s name is “name”.

1] mysql_fetch_assoc() mysql_fetch_assoc() fetch the data from database through associative array. [code type=php] <html> <?php $query=mysql_query(“select * from tb”); $query1=mysql_fetch_assoc($query); echo $query1[‘name’]; ?> </html> [/code]   2] mysql_fetch_row() mysql_fetch_row() function will fetches the data from database through numeric array. [code type=php] <html> <?php $query=mysql_query(“select * from tb”); $query1=mysql_fetch_row($query); echo $query1[0]; ?> </html> [/code]   3] mysql_fetch_array() mysql_fetch_array() function fetches data from database through both numeric array and associative array. [code type=php] <html> <?php $query=mysql_query(“select * from tb”); /*Associative array to get data */ $query1=mysql_fetch_array($query); echo $query1[‘name’]; /* Numeric array to get data */ echo $query1[0]; ?> </html> [/code]   4] mysql_fetch_object() mysql_fetch_object() function fetches the data from database through object. [code type=php] <html> <?php $query=mysql_query(“select * from tb”); $query1=mysql_fetch_object($query); echo $query1->name; ?> </html> [/code]   5] mysql_result() [code type=php] <html> <?php $query=mysql_query(“select name from tb”); $query1=mysql_result($query,0); echo $query1; ?> </html> [/code]

One thought on “5 PHP Functions to Fetch Data from Database

Leave a Reply

Your email address will not be published. Required fields are marked *