How to retrieve data from MySQL database using PHP ? Last Updated : 10 Mar, 2022 Comments Improve Suggest changes Like Article Like Report There are steps to understand for retrieving the data from the MySQL database. Approach: Create the database, then create the table for data.Enter the rows in the table. You have to connect to the database. 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 Comment More infoAdvertise with us Next Article How to retrieve data from MySQL database using PHP ? rohanmittal1366 Follow Improve Article Tags : PHP Geeks-Premier-League-2022 PHP-function PHP-MySQL HTML-Questions PHP-Questions +1 More Similar Reads How to Retrieve Data from Database in Rails? In this article, we'll be creating a new Rails project, setting up a database, adding demo data, and retrieving that data to display in your application. Steps to Retrieve Data from DatabaseStep 1: Create a Project You can create a new Rails project using the following command in your terminal. rail 3 min read 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 Insert JSON data into MySQL database using PHP? To insert JSON data into MySQL database using PHP, use the json_decode function in PHP to convert JSON object into an array that can be inserted into the database. Here, we are going to see how to insert JSON data into MySQL database using PHP through the XAMPP server in a step-by-step way. JSON Str 3 min read How to make a Todo App using PHP & MySQL ? To create a Todo App using PHP and MySQL, you'll first need to set up a MySQL database to store the tasks. Then, use PHP to create a web interface where users can add, edit, and delete tasks. PHP will handle the backend logic, such as connecting to the database and performing CRUD operations. Finall 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 How to fix MySQL Error 1046 No database selected? MySQL error 1046, which stands for âNo Database Selected,â is one of the most common errors that can prevent database operations. It happens when you run a query and donât specify the target database. In this article, we will discuss "How to resolve, Error 1046: No database selected". The first and 9 min read Insert data from one table to another table using PHP In this article, we are going to insert data into another table from the existing table using PHP. Requirements: XAMPP Webserver PHP stands for Hypertext preprocessor. MySQL is a database query language for performing database operations. We are going to insert data from one table into another table 2 min read Like