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.

1
<html> <?php $query=mysql_query("select * from tb"); $query1=mysql_fetch_assoc($query); echo $query1['name']; ?> </html>

  2] mysql_fetch_row() mysql_fetch_row() function will fetches the data from database through numeric array.

1
<html> <?php $query=mysql_query("select * from tb"); $query1=mysql_fetch_row($query); echo $query1[0]; ?> </html>

  3] mysql_fetch_array() mysql_fetch_array() function fetches data from database through both numeric array and associative array.

1
<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>

  4] mysql_fetch_object() mysql_fetch_object() function fetches the data from database through object.

1
<html> <?php $query=mysql_query("select * from tb"); $query1=mysql_fetch_object($query); echo $query1->name; ?> </html>

  5] mysql_result()

1
<html> <?php $query=mysql_query("select name from tb"); $query1=mysql_result($query,0); echo $query1; ?> </html>

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 *