How to fetch data from mysql using PHP



 




How to fetch data from mysql using PHP

In previous tutorial I explained how to insert data into mysql using PHP. Now in this tutorial i will explain how to fetch data from mysql using PHP.

We can fetch the data from mysql using following methods given below :

  • mysqli_fetch_array
  • mysqli_fetch_row
  • mysqli_fetch_assoc
  • mysqli_fetch_object

First Create a database with name demo. Now create a table with name data.

Here the strcuture of table

Code for mysql connection with php(config.php)

Fetch data from mysql using mysqli_fetch_array

Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array.This function will actually return an array with both the contents of mysqli_fetch_row and mysqli_fetch_assoc merged into one. It will both have numeric and string keys.

Fetch data from mysql using mysqli_fetch_row

Mysqli_fetch_row fetch result row an numeric way. This function return a row where the value will come in the order as they are defined in the query, and the keys will span from 0 to one less than the number of columns selected.

Fetch data from mysql using mysqli_fetch_assoc()

Fetch a result row as an associative array.This function will return a row as an associative array where the column names will be the keys storing corresponding value.

Fetch data from mysql using mysqli_fetch_object()

Fetch a result row as an object.