How to retrieve data from MySQL database using PHP ?
Last Updated :
10 Mar, 2022
There are steps to understand for retrieving the data from the MySQL database.Â
Approach:
Now we understand each and every step as shown below.
 Example 1: In this. we use PHPMyAdmin for the database handling. Start the server in the XAMPP as shown in the below image
Making sure that both Apache and MySQL are showing green color, means that the server is working.
XAMPP PANEL
After that create the database in the PHPMyAdmin. Open the below URL.
http://localhost/phpmyadmin/index.php
Creating the database:
creating the database in MySQL PHPMyAdmin
Create the table: Execute the SQL query in the "gfg" database.
CREATE TABLE student
(
name varchar(20),
branch varchar(20),
roll_no INT
);
Enter the data in the table.
INSERT INTO `student` ( `name`, `branch`, `roll_no`)
VALUES ( 'Rohan', 'CSE', '1' );
After the data is inserted, the table will look like this.
After entering the data in the table
PHP Code: Run the PHP script as shown below to fetch the data from the database.
PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gfg";
// connect the database with the server
$conn = new mysqli($servername,$username,$password,$dbname);
// if error occurs
if ($conn -> connect_errno)
{
echo "Failed to connect to MySQL: " . $conn -> connect_error;
exit();
}
$sql = "select * from student";
$result = ($conn->query($sql));
//declare array to store the data of database
$row = [];
if ($result->num_rows > 0)
{
// fetch all data from db into array
$row = $result->fetch_all(MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<style>
td,th {
border: 1px solid black;
padding: 10px;
margin: 5px;
text-align: center;
}
</style>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Branch</th>
<th>Roll Number</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($row))
foreach($row as $rows)
{
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['branch']; ?></td>
<td><?php echo $rows['roll_no']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<?php
mysqli_close($conn);
?>
Output:
Output of the above PHP script
Similar Reads
How to Update Data in MySQL Database Table Using PHP? Updating data in a MySQL database table using PHP is a fundamental aspect of web development, particularly in applications where user interactions involve modifying existing records. This guide delves into the process of updating data in a MySQL database table using PHP, covering database connection
3 min read
How to fetch data from the database in PHP ? Database operations in PHP are a very crucial thing that is especially needed in CRUD (Create, Read, Update and Delete) operations. In this article, we will discuss the Read part i.e. data fetching from database. There are two ways to connect to a database using PHP. They are as follows. MySQLi ("i"
4 min read
How to make a connection with MySQL server using PHP ? MySQL is a widely used database management system that may be used to power a wide range of projects. One of its main selling features is its capacity to manage large amounts of data without breaking a sweat. There are two approaches that can be used to connect MySQL and PHP code, which are mentione
3 min read
How to Extract Data from an XML File Using PHP ? XML, which stands for Extensible Markup Language, is a data storage format that is easily searchable and understandable. It simplifies the process of storing, retrieving, and displaying data in informative applications. It is widely used in web applications. In this article, we will learn how to ext
3 min read
How to get form data using POST method in PHP ? PHP provides a way to read raw POST data of an HTML Form using php:// which is used for accessing PHPâs input and output streams. In this article, we will use the mentioned way in three different ways. We will use php://input, which is a read-only PHP stream. We will create a basic HTML form page wh
2 min read