How to fetch data from database in php using function

In this article, you are going to learn about the MySQL functions in php to fetch data from the database. To perform this task, you can use anyone of the following MySQL functions according to the output you desire..

  1. mysqli_fetch_array()
  2. mysqli_fetch_assoc()
  3. mysqli_fetch_row()
  4. mysqli_fetch_object()
  5. mysqli_fetch_field()
  6. mysqli_fetch_lengths()

These mysql functions are used to extract the necessary information or data from the SQL database. Fetching data from the database helps the web developers to get necessary details and to manipulate it according to their need. Join our php training with mysql to learn its all other functionality in database connectivity. To use these data fetching MySQL functions, first you will need to create a table and add some information into the table by using the SQL queries. Then only these functions can be used to retrieve data from the created database.

MYSQLI_FETCH_ARRAY()

This is used to fetch data from a row of a table. The data can be fetched as an associative array, numeric array or both depending on the type of result you want to generate.

Syntax : mysqli_fetch_array($result, $result_type);

Two parameters are used in this function whose description is shown below

  • $result- It denotes the output (resource) of the php script. This is generated by using the mysqli_query() function.
  • $result_type- It denotes the type of result/output that is generated. You can put the following three values according to the type of result you desire.
    (a) MYSQLI_ASSOC (used to get the output only as an associative array)
    (b) MYSQLI_NUM (used to get output only as an numeric array)
    (c) MYSQLI_BOTH (used to get output as both associative and numeric array)

Result/Return Value : This function will return array values of a row if the row is present, and will return False if the row is not present.

How to fetch data from database in php using function

Example : An example of mysqli_fetch_array() function is shown below.

How to fetch data from database in php using function

Output : You will get the following output using the above php script.

MYSQLI_FETCH_ASSOC()

This function is used to fetch the data from the table only as an associative array. A single parameter $outcome is used in this function as shown below in the syntax. It denotes the output of the php script to be generated by using the mysqli_query() function.

Syntax : mysqli_fetch_assoc($result);

Result/Return Value : This function will return associative array corresponding to the values of a row if the row is present, and will return False if the row is not present.

How to fetch data from database in php using function

Example : We have shown below an example to retrieve data from a database using mysqli_fetch_assoc() function.

How to fetch data from database in php using function

Output : The following output will be generated by using the above php codes.

MYSQLI_FETCH_ROW()

mysqli_fetch_row() function is used to extract data from a row of the database table only as an numeric array. Like mysqli_fetch_assoc() function, it also uses a single parameter $outcome which denotes the same as in the case above.

Syntax : mysqli_fetch_row($result);

Result/Return Value : The successful execution of this function will result a numeric array having values of the row to be fetched. And, it will return False, if it doesn’t find the row.

How to fetch data from database in php using function

Example : An example of mysqli_fetch_row() function is shown below.

How to fetch data from database in php using function

Output : The output will look something like this.

MYSQLI_FETCH_OBJECT()

This function is used to extract the data from a row of the database table as an object.

Syntax : mysqli_fetch_object($result, $class_name, $additional parameter);

In this function, three parameters are used which are described below.

  • $result- the desired output (resource) being generated using mysqli_query() function.
  • $class_name- it is a string value that denotes the name of the class. If this parameter is not defined, the function will return stdClass object.
  • $additional parameter- this is an optional parameter which represents an array.

Result/Return Value : The successful execution of the function will result in an object value equivalent to the value of the row being fetched. And, it will return False, if the row is unavailable. It must be noted that the class name of the object must be declared.

How to fetch data from database in php using function

Example : Following is an example of mysqli_fetch_object() that is used to fetch the data from a database table.

Output : You will get the following results by using the above function. 

MYSQLI_FETCH_FIELD()

This MySQL function is used to fetch the data from a column of the database table.

Syntax : mysqli_fetch_field($result, $field_offset);

Two parameters are used in this function that are discussed below.

  • $result- it represents the outcome (resource) of the php script using mysqli_query() function.
  • $field_offset- it is a numerical value starts at ‘0’ which if not defined will result in the execution of the next information.

Result/Return Value : It will return an object value equivalent to the value of the row being fetched.

Example : We have shown below an example of mysqli_fetch_field() function in php.

How to fetch data from database in php using function

Output : You will get the results as shown in the image below.

MYSQLI_FETCH_LENGTHS()

This MySQL function is used in php to retrieve the length of the output data.

Syntax : mysqli_fetch_lengths($result);

Result/Return Value : On the successful execution of the function, you will get an array value of lengths equivalent to the value of each row or a False statement in case the execution is unsuccessful.

How to fetch data from database in php using function

Example : A simple example of mysqli_fetch_lengths() function is shown in the image below.

How to fetch data from database in php using function

Output : The output of the above php code will look like this.

We have provided you the best possible information about the MySQL functions in php that are used to retrieve information from the database table. Hope you liked this article. For further updates, stay tuned to our php blogs. 

How get fetch data from database in PHP?

Retrieve or Fetch Data From Database in PHP.
SELECT column_name(s) FROM table_name..
$query = mysql_query("select * from tablename", $connection);.
$connection = mysql_connect("localhost", "root", "");.
$db = mysql_select_db("company", $connection);.
$query = mysql_query("select * from employee", $connection);.

Which of the following function is used to fetch a data from database?

MYSQLI_FETCH_FIELD() This MySQL function is used to fetch the data from a column of the database table.

How fetch data from database in PHP and display in table?

Use the following steps for fetch/retrieve data from database in php and display in html table:.
Step 1 – Start Apache Web Server..
Step 2 – Create PHP Project..
Step 3 – Execute SQL query to Create Table..
Step 4 – Create phpmyadmin MySQL Database Connection File..
Step 5 – Create Fetch Data PHP File From Database..

What does Mysql_fetch_row () function do?

The fetch_row() / mysqli_fetch_row() function fetches one row from a result-set and returns it as an enumerated array.