PHP program to fetch data from localhost server database using XAMPP
Last Updated :
10 Apr, 2025
In this article, we will see how we can display the records by fetching them from the MySQL database using PHP.
Approach: Make sure you have an XAMPP server or WAMP server installed on your machine. In this article, we will be using the XAMPP server.
XAMPP is a free and open-source cross-platform web server solution stack package developed by Apache which allows a web application to be easily tested on a local web server. Here, we can manually create a relational database and store data in tabular form by going to this link. But to operate on localhost or for storing data first we have to start Apache and MySQL from the XAMPP control panel. Let, for example, the database name is server, the table name is user_info having column names as ID, First Name, Username, and Password and we have to fetch the data stored there. So, below is the PHP program whose task is to fetch data.
Follow the steps to fetch data from the Database using PHP:
Create Database
Create a database using PHPMyAdmin, the database is named "geeksforgeeks" here. You can give any name to your database.
create database "geeksforgeeks"Create Table
Create a table named 'user_info'. The table contains four fields:
- id - int(11) - primary key – auto increment
- first_name - varchar(100)
- last_name - varchar(100)
- gfg_username - varchar(100)
Your table structure should look like this:
the table structure of "user_info"Or you can create a table by copying and pasting the following code into the SQL panel of your PHPMyAdmin.
CREATE TABLE IF NOT EXISTS `user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`gfg_username` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
To do this from the SQL panel refer to the following screenshot:
create a table 'user_info" from the SQL panelInsert Records
We will now insert some records into our table. Here we are inserting 4 records. You can add multiple records.
records in our tableCopy and paste the following code into the SQL panel to insert records into the table.
INSERT INTO `user_info` (`first_name`, `last_name`, `gfg_username`) VALUES ('Rohit', 'Kumar', 'rohitk987'),
('Nisha', 'Jadhav', 'nishajadhav001'), ('Aayush', 'Joshi', 'geeky1aayush'), ('Shweta', 'Pawar', 'shwetap12gfg');
inserting recordsCreating folder and files
We will now create our project folder named "GeeksforGeeks". Create index.php and database.php files. Keep your main project folder (for example here.. GeeksforGeeks) in the "C://xampp/htdocs/", if you are using XAMPP or "C://wamp64/www/" folder if you are using the WAMP server respectively. The folder structure should look like this:
folder structuredatabase.php: Code for connection with the database.
PHP
<?php
// this php script is for connecting with database
// data has to be fetched from local server
// Username is root
$user = 'root';
$password = '';
// Database name is geeksforgeeks
$database = 'geeksforgeeks';
// Server is localhost with
// port number 3306
$servername='localhost:3306';
$mysqli = new mysqli($servername, $user,
$password, $database);
// Checking for connections
if (!$mysqli){
echo "Connection Unsuccessful!!!";
}
?>
index.php: Code for displaying the records.
PHP
<?php
// going to use above code
require 'database.php';
// printing column name above the data
echo 'ID'.' '.'First Name'.' '.'Last Name'.' '.'GFG Username'.'<br>';
// sql query to fetch all the data
$query = "SELECT * FROM `user_info`";
// mysql_query will execute the query to fetch data
if ($is_query_run = mysqli_query($mysqli,$query))
{
// echo "Query Executed";
// loop will iterate until all data is fetched
while ($query_executed = $is_query_run->fetch_assoc())
{
// these four line is for four column
echo $query_executed['id'].' ';
echo $query_executed['first_name'].' ';
echo $query_executed['last_name'].' ';
echo $query_executed['gfg_username'].'<br>';
}
}
else
{
echo "Error in execution!";
}
?>
Output:
ID First Name Last Name GFG Username
1 Rohit Kumar rohitk987
2 Nisha Jadhav nishajadhav001
3 Aayush Joshi geeky1aayush
4 Shweta Pawar shwetap12gfg
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
How to fetch data from Database in PHP PDO using loop ? The PDO (PHP Data Objects) defines the lightweight, consistent interface for accessing databases in PHP. Approach: Make sure you have XAMPP or WAMP installed on your windows machine. In case you're using Linux then install the LAMP server. In this article, we will be using the XAMPP server. Follow
4 min read
How to Export data to CSV file from Database using XAMPP ? In this article, we are going to load the data present in the database (MySQL) into CSV file. Display the data in the database and also export that data into CSV file. We are using XAMPP tool to store locally in a database. XAMPP stands for  cross-platform, Apache, MySQL, PHP, and Perl. It is among
3 min read
How to read data from a file stored in XAMPP webserver using PHP ? We have given a file stored on XAMPP server and the task is to read the file from server and display the file content on the screen using PHP. We use some PHP functions to solve this problem. File: A file is set of data stored in a disk in different formats. For example - .txt, .exe, .pdf etc fopen
2 min read
How to fetch data from localserver database and display on HTML table using PHP ? In this article, we will see how we can display the records in an HTML table by fetching them from the MySQL database using PHP. Approach: Make sure you have XAMPP or WAMP server installed on your machine. In this article, we will be using the WAMP server. WAMP Server is open-source software for th
4 min read
How to Insert Form Data into Database using PHP ? Inserting form data into a MySQL database using PHP allows web applications to store user inputs like registrations or feedback. By collecting and validating the data from HTML forms, PHP securely inserts it into the database, ensuring data integrity and protection against security risks.Here, we wi
5 min read