Fetch MYSQL table data – PHP

Welcome to all,
Here we discuss one useful topic related to MYSQL,fetch the data from data base in MySQL using PHP.
From the MYSQL database the data can be fetched by executing SQL SELECT statement through PHP function mysql_query. The mysqli_fetch_array() or mysql_fetch_array() function is most frequently option. This function returns row as an associative array, a numeric array, or both. This function returns FALSE if there are no more rows.



The basic syntax of the SELECT query can be given with:

SELECT column_name(s) FROM table_name

Basic Content

  • mysql_connect() : Used to open connection in MySQL
  • mysql_error() : this returns the text of the error message from previous MySQL operation and empty string if no error occurred.
  • mysql_select_db() : Sets the current active database on the server which is associated with specified link identifier returned by mysql_connect().



  • mysqli_query() : This sends a query to current active database on the server. It takes SQL query as parameter.
  • mysqli_fetch_array():The mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both.

Below is a simple example to fetch records from Employee_table table.

There are three steps in here. First create Employee_table in fetch_demo database. Second, insert the data to the Employee_table and last fetch the data from Employee_table.

Create Employee_table

CREATE TABLE Employee_table
(Employee_ID INTEGER PRIMARY KEY,
Employee_First_Name CHAR(200),
Employee_Last_Name CHAR(200),
Employee_Email_ID CHAR(100)
);




Insert the data to Employee_table

INSERT INTO Employee_table(Employee_ID,Employee_First_Name,Employee_Last_Name,Employee_Email_ID) VALUES (01, ‘Priya’, ‘Mary’,’priyamary@gmail.com’);
INSERT INTO Employee_table(Employee_ID,Employee_First_Name,Employee_Last_Name,Employee_Email_ID) VALUES (02, ‘Princy’, ‘Mary’,’princymary@gmail.com’);
INSERT INTO Employee_table(Employee_ID,Employee_First_Name,Employee_Last_Name,Employee_Email_ID) VALUES (03, ‘Preethi’, ‘Sunny’,’preethi_sunny@gmail.com’);
INSERT INTO Employee_table(Employee_ID,Employee_First_Name,Employee_Last_Name,Employee_Email_ID) VALUES (04, ‘Pritty’, ‘Mary’,’prittyamaryvarghese@gmail.com’);

Fetch the data from Employee_table

       0)
			{
				echo "";
				echo "";
                echo "";
                echo "";
                echo "";
                echo "";
				echo "";
				while($row = mysqli_fetch_array($result))
				{
					echo "";
					echo "";
					echo "";
					echo "";
					echo "";
					echo "";
				}
				echo "
Employee_IDEmployee_First_NameEmployee_Last_NameEmployee_Email_ID
" . $row['Employee_Id'] . "" . $row['Employee_First_Name'] . "" . $row['Employee_Last_Name'] . "" . $row['Employee_Email_ID'] . "
"; } else { echo "No records matching your query were found."; } } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($con); } mysqli_close($con); ?>

Fetch all the data from Employee_table. And display in table. Fetch the data out of the fetch_demo database.
There are several mathods are available in fetching data from MYSQL database. In above example mysql_fetch_array() function is used.

The Output like:




Employee_ID Employee_First_Name Employee_Last_Name Employee_Email_ID
01 Priya Mary priyamary@gmail.com
02 Princy Mary princymary@gmail.com
03 Preethi Sunny preethi_sunny@gmail.com
04 Pritty Mary prittyamaryvarghese@gmail.com

All the datas are fetched from table Employee_table. There are four fields are available, they are:Employee_ID,Employee_First_Name,Employee_Last_Name,Employee_Email_ID.
Fetched datas are displayed in table.

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Leave a Reply

Your email address will not be published.