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
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
TCP/IP Model
The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Basics of Computer Networking
A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Unified Modeling Language (UML) Diagrams
Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
Waterfall Model - Software Engineering
The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because
13 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read