PHP MySQL Introduction

11/22/2025

markdown

Go Back

PHP MySQL Introduction: Getting Started with Database Programming

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


markdown

What is MySQL?

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.


Why Use PHP with MySQL?

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.


Setting Up PHP and MySQL

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


Create a Sample Database and Table

  1. Open phpMyAdmin

  2. Create a database: php_tutorial

  3. 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
);

Connecting PHP to MySQL (Using mysqli)

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


Insert Data into MySQL Using 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.


Fetch Data from MySQL Using PHP

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);
?>

mysqli vs PDO

PHP supports two major MySQL interfaces:

FeaturemysqliPDO
Database SupportOnly MySQL12+ databases
Prepared StatementsYesYes
Object-OrientedYesYes
Ease of UseBeginner-friendlyMore flexible

For beginners → mysqli
For advanced apps → PDO


Common PHP MySQL Operations

  • Insert data → INSERT INTO

  • Fetch data → SELECT

  • Update data → UPDATE

  • Delete data → DELETE

Used in:

  • Login systems

  • Admin dashboards

  • Product listings

  • Search systems


Conclusion

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.


Meta Description

Beginner-friendly introduction to PHP MySQL. Learn how to connect PHP to MySQL, insert data, and fetch records using mysqli.

Meta Keywords

php mysql introduction, php mysqli tutorial, php connect to mysql, php database examples, php mysql beginners guide

Alt Tag

"PHP connecting to MySQL database tutorial for beginners"