How to Create a Database Using XAMPP: Complete Step-by-Step Guide
How to Create a Database Using XAMPP: Complete Step-by-Step Guide
Creating a database is essential for web development. XAMPP makes it incredibly easy to set up a local database server on your computer without complex configurations.
This guide walks you through every single step of installing XAMPP, creating databases, and managing them using phpMyAdmin. No steps are skipped.
Table of Contents
- What is XAMPP and Why Use It?
- Step 1: Download and Install XAMPP
- Step 2: Start Apache and MySQL Services
- Step 3: Access phpMyAdmin
- Step 4: Create Your First Database
- Step 5: Create Tables in Your Database
- Step 6: Insert Data into Tables
- Step 7: Connect to Database Using PHP
- Common Issues and Solutions
What is XAMPP and Why Use It?
XAMPP is a free, open-source software package that includes:
- Apache - Web server
- MySQL - Database server
- PHP - Programming language
- phpMyAdmin - Database management tool
Why Developers Use XAMPP
- Free and open-source
- Easy to install - One-click installation
- Cross-platform - Works on Windows, Mac, Linux
- Perfect for local development - Test websites before going live
- No internet required - Everything runs locally
Step 1: Download and Install XAMPP
Download XAMPP
-
Visit the official website: https://www.apachefriends.org
-
Click the download button for your operating system:
- Windows (most common)
- Mac OS X
- Linux
-
Choose the latest version (usually PHP 8.x)
-
Download will start automatically (file size: ~150-200 MB)
Install XAMPP on Windows
-
Locate the downloaded file (usually in Downloads folder)
- File name:
xampp-windows-x64-8.x.x-installer.exe
- File name:
-
Right-click the installer → Run as Administrator
-
Windows Security warning may appear:
- Click "Yes" to allow installation
-
XAMPP Setup Wizard opens:
- Click "Next"
-
Select Components screen:
- Keep all default selections checked:
- ✅ Apache
- ✅ MySQL
- ✅ PHP
- ✅ phpMyAdmin
- Click "Next"
- Keep all default selections checked:
-
Installation folder screen:
- Default:
C:\xampp - Keep this default (recommended)
- Click "Next"
- Default:
-
Language selection:
- Choose English
- Click "Next"
-
Ready to Install screen:
- Click "Next"
-
Installation progress:
- Wait 2-5 minutes
- Files are being extracted and installed
-
Installation complete:
- ✅ Check "Do you want to start the Control Panel now?"
- Click "Finish"
Expected Output
After installation:
- XAMPP is installed in
C:\xampp - XAMPP Control Panel opens automatically
- Desktop shortcut is created (optional)
Step 2: Start Apache and MySQL Services
Open XAMPP Control Panel
If not already open:
- Go to
C:\xampp - Double-click
xampp-control.exe - Or use the desktop shortcut
Start Required Services
The XAMPP Control Panel shows all available services:
-
Locate Apache in the list
- Click "Start" button next to Apache
- Wait for status to show green background
- Port numbers appear:
80, 443
-
Locate MySQL in the list
- Click "Start" button next to MySQL
- Wait for status to show green background
- Port number appears:
3306
Expected Output
When services are running:
- Apache: Green background, shows "Running" status
- MySQL: Green background, shows "Running" status
- Both services show PID (Process ID) numbers
Troubleshooting Port Conflicts
If Apache won't start:
Problem: Port 80 is already in use (often by Skype or IIS)
Solution:
- Click "Config" button next to Apache
- Select "Apache (httpd.conf)"
- Find line:
Listen 80 - Change to:
Listen 8080 - Save and close
- Click "Start" again
If MySQL won't start:
Problem: Port 3306 is already in use
Solution:
- Click "Config" button next to MySQL
- Select "my.ini"
- Find line:
port=3306 - Change to:
port=3307 - Save and close
- Click "Start" again
Step 3: Access phpMyAdmin
Open phpMyAdmin
- Open your web browser (Chrome, Firefox, Edge, etc.)
- Type in the address bar:
http://localhost/phpmyadmin - Press Enter
Expected Output
You should see:
- phpMyAdmin dashboard loads
- Left sidebar shows existing databases
- Top navigation shows tabs: Databases, SQL, Status, etc.
- Welcome screen in the main area
Default Login (if prompted)
- Username:
root - Password: (leave blank)
- Click "Go"
Step 4: Create Your First Database
Using phpMyAdmin Interface
-
Click "Databases" tab at the top
-
Find "Create database" section
-
Enter database name in the text field:
- Example:
my_first_database - Use lowercase, no spaces
- Use underscores instead of spaces
- Example:
-
Select Collation (optional):
- Default:
utf8mb4_general_ci(recommended) - This handles international characters
- Default:
-
Click "Create" button
Expected Output
After creation:
- Success message appears: "Database my_first_database has been created"
- New database appears in left sidebar
- Database is empty (0 tables)
Create Multiple Databases
Repeat the process for different projects:
my_blog_database
ecommerce_store
portfolio_site
test_database
Step 5: Create Tables in Your Database
Select Your Database
- Click your database name in the left sidebar
- Main area shows "No tables found in database"
Create a Table
- Find "Create table" section
- Enter table name: Example:
users - Enter number of columns: Example:
5 - Click "Go"
Define Table Structure
A form appears with rows for each column:
Example: Creating a "users" table
| Column Name | Type | Length | Default | Attributes | Null | Index | A_I |
|---|---|---|---|---|---|---|---|
| id | INT | 11 | None | UNSIGNED | ❌ | PRIMARY | ✅ |
| username | VARCHAR | 50 | None | - | ❌ | - | ❌ |
| VARCHAR | 100 | None | - | ❌ | UNIQUE | ❌ | |
| password | VARCHAR | 255 | None | - | ❌ | - | ❌ |
| created_at | TIMESTAMP | - | CURRENT_TIMESTAMP | - | ❌ | - | ❌ |
Step-by-step for each column:
Column 1: id
- Name:
id - Type: Select
INT - Length:
11 - Attributes: Select
UNSIGNED - Null: Uncheck (not null)
- Index: Select
PRIMARY - A_I (Auto Increment): Check ✅
Column 2: username
- Name:
username - Type: Select
VARCHAR - Length:
50 - Null: Uncheck (not null)
Column 3: email
- Name:
email - Type: Select
VARCHAR - Length:
100 - Null: Uncheck (not null)
- Index: Select
UNIQUE
Column 4: password
- Name:
password - Type: Select
VARCHAR - Length:
255 - Null: Uncheck (not null)
Column 5: created_at
- Name:
created_at - Type: Select
TIMESTAMP - Default: Select
CURRENT_TIMESTAMP - Null: Uncheck (not null)
Save the Table
- Scroll down
- Click "Save" button at the bottom
Expected Output
- Success message: "Table users has been created"
- Table appears in left sidebar under your database
- Table structure is displayed
Step 6: Insert Data into Tables
Method 1: Using phpMyAdmin Interface
- Click your table name (
users) in the sidebar - Click "Insert" tab at the top
- Fill in the form:
Example data:
- id: (leave blank - auto increment)
- username:
john_doe - email:
john@example.com - password:
hashed_password_here - created_at: (leave default)
- Click "Go" at the bottom
Insert Multiple Rows
- After first insert, form reappears
- Enter another user:
- username:
jane_smith - email:
jane@example.com - password:
another_hashed_password
- username:
- Click "Go"
Method 2: Using SQL Query
- Click "SQL" tab at the top
- Enter SQL query:
INSERT INTO users (username, email, password) VALUES
('john_doe', 'john@example.com', 'hashed_password_1'),
('jane_smith', 'jane@example.com', 'hashed_password_2'),
('bob_wilson', 'bob@example.com', 'hashed_password_3');
- Click "Go"
View Your Data
- Click "Browse" tab
- See all inserted data in a table format
Expected Output
- Data is inserted successfully
- Browse tab shows all rows
- Each row has a unique auto-incremented ID
Step 7: Connect to Database Using PHP
Create a PHP Connection File
- Open your text editor (VS Code, Notepad++, etc.)
- Create new file:
db_connect.php - Save in:
C:\xampp\htdocs\
PHP Connection Code
<?php
// Database configuration
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'my_first_database';
// Create connection
$conn = new mysqli($host, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to database!";
// Close connection when done
// $conn->close();
?>
Test the Connection
- Save the file
- Open browser
- Go to:
http://localhost/db_connect.php
Expected Output
Browser displays: "Connected successfully to database!"
Fetch Data from Database
Create fetch_users.php:
<?php
// Include connection file
include 'db_connect.php';
// SQL query
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
// Check if data exists
if ($result->num_rows > 0) {
echo "<h2>Users List</h2>";
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th><th>Created At</th></tr>";
// Output data
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["username"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "<td>" . $row["created_at"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No users found";
}
$conn->close();
?>
Test Data Fetching
- Save the file in
C:\xampp\htdocs\ - Open browser
- Go to:
http://localhost/fetch_users.php
Expected Output
Browser displays:
- "Users List" heading
- Table with all users from database
- Columns: ID, Username, Email, Created At
Common Issues and Solutions
Issue 1: XAMPP Control Panel Won't Open
Solution:
- Right-click
xampp-control.exe - Select "Run as Administrator"
- If still fails, restart your computer
Issue 2: Apache Won't Start (Port 80 Busy)
Solution:
- Change Apache port to 8080 (see Step 2)
- Or stop conflicting programs (Skype, IIS)
Issue 3: MySQL Won't Start
Solution:
- Check if MySQL service is already running in Windows Services
- Stop the existing MySQL service
- Or change MySQL port in XAMPP
Issue 4: "Access Denied" Error in phpMyAdmin
Solution:
- Username:
root - Password: (leave blank)
- If changed, use your custom password
Issue 5: PHP File Downloads Instead of Running
Solution:
- Make sure Apache is running
- Save PHP files in
C:\xampp\htdocs\ - Access via
http://localhost/filename.php(not file path)
Issue 6: Database Connection Failed
Solution:
- Verify MySQL is running in XAMPP Control Panel
- Check database name spelling
- Ensure username is
rootand password is blank (default)
Best Practices
Security Tips
-
Change root password:
- Open phpMyAdmin
- Click "User accounts"
- Edit root user
- Set a strong password
-
Don't use root in production:
- Create separate users for each database
- Grant only necessary permissions
-
Backup regularly:
- Export databases from phpMyAdmin
- Save
.sqlfiles
Database Naming Conventions
- Use lowercase letters
- Use underscores instead of spaces
- Be descriptive:
blog_postsnotbp - Avoid special characters
Table Design Tips
- Always include an id column (primary key, auto increment)
- Use appropriate data types (INT for numbers, VARCHAR for text)
- Add timestamps (created_at, updated_at)
- Set NOT NULL for required fields
Next Steps
Now that you have XAMPP set up:
- Practice creating databases for different projects
- Learn SQL queries (SELECT, INSERT, UPDATE, DELETE)
- Build PHP applications that interact with databases
- Explore phpMyAdmin features (import, export, search)
- Study database relationships (foreign keys, joins)
Useful Resources
- phpMyAdmin Documentation: https://docs.phpmyadmin.net
- MySQL Tutorial: https://www.mysqltutorial.org
- PHP MySQL Guide: https://www.php.net/manual/en/book.mysqli.php
You now have a fully functional local database server! Start building database-driven applications with confidence.
You can also check our official website: Codecraft Academy for more blogs and courses.
Comments (0)
Leave a Comment
No comments yet. Be the first to comment!