PHP MySQL Introduction
markdown
When you move from simple PHP pages to dynamic web applications, you need a way to store and retrieve data—like users, products, orders, or blog posts. That’s where MySQL comes in.
This tutorial introduces you to PHP + MySQL, explaining:
What MySQL is
Why PHP and MySQL are used together
How to connect PHP to a MySQL database
How to insert and fetch data with simple examples
MySQL is an open-source relational database management system (RDBMS) used to store structured data in tables.
Common use cases:
User login & registration systems
Blogs & CMS platforms
E-commerce websites
Analytics dashboards
MySQL powers popular platforms like WordPress, Drupal, Joomla, and many custom PHP applications.
PHP and MySQL are a classic backend combination because:
Seamless Integration using mysqli or PDO
Fast and reliable on all hosting environments
Open source and widely available
Ideal for dynamic websites that require data storage
This combination forms the core of the popular LAMP/WAMP/XAMPP stack.
Use one of these packages for quick setup:
XAMPP (Windows/Mac/Linux)
WAMP (Windows)
MAMP (macOS)
These include:
Apache (Web Server)
PHP
MySQL
phpMyAdmin
After installation:
Start Apache and MySQL
Open: http://localhost/phpmyadmin
Open phpMyAdmin
Create a database: php_tutorial
Create a table by running this SQL:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Create a file named db_connect.php:
<?php
$host = "localhost";
$user = "root"; // default for XAMPP
$pass = ""; // default password
$dbname = "php_tutorial";
$conn = mysqli_connect($host, $user, $pass, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
?>
Open this file:http://localhost/db_connect.php
Create insert_user.php:
<?php
include "db_connect.php";
$name = "Shubham";
$email = "[email protected]";
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
if (mysqli_query($conn, $sql)) {
echo "User inserted successfully!";
} else {
echo "Error: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Note: For real applications, use prepared statements to prevent SQL injection.
Create list_users.php:
<?php
include "db_connect.php";
$sql = "SELECT id, name, email, created_at FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<h2>User List</h2>";
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row['id'] . " | ";
echo "Name: " . $row['name'] . " | ";
echo "Email: " . $row['email'] . " | ";
echo "Created: " . $row['created_at'] . "<br>";
}
} else {
echo "No users found.";
}
mysqli_close($conn);
?>
PHP supports two major MySQL interfaces:
| Feature | mysqli | PDO |
|---|---|---|
| Database Support | Only MySQL | 12+ databases |
| Prepared Statements | Yes | Yes |
| Object-Oriented | Yes | Yes |
| Ease of Use | Beginner-friendly | More flexible |
For beginners → mysqli
For advanced apps → PDO
Insert data → INSERT INTO
Fetch data → SELECT
Update data → UPDATE
Delete data → DELETE
Used in:
Login systems
Admin dashboards
Product listings
Search systems
PHP + MySQL is the foundation of dynamic web development. With MySQL storing data and PHP processing it, you can build login systems, blogs, e-commerce platforms, and much more.
Start with basic queries, then explore prepared statements, pagination, and CRUD applications.
Beginner-friendly introduction to PHP MySQL. Learn how to connect PHP to MySQL, insert data, and fetch records using mysqli.
php mysql introduction, php mysqli tutorial, php connect to mysql, php database examples, php mysql beginners guide
"PHP connecting to MySQL database tutorial for beginners"