PHP MySQL Create Table – Step-by-Step Guide with Best Practices
Creating tables in MySQL using PHP is a crucial step in building dynamic web applications. This guide will show you how to create a table using PHP, following best practices to ensure security and efficiency.
Connect to MySQL Database
Before creating a table, ensure you are connected to the MySQL database.
Using mysqli
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase"; // Change this to your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Using PDO
<?php
$dsn = "mysql:host=localhost;dbname=mydatabase";
$username = "root";
$password = "";
try {
$conn = new PDO($dsn, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Create a MySQL Table
Example Using mysqli
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL to create a table
$sql = "CREATE TABLE users (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table 'users' created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
Example Using PDO
<?php
$dsn = "mysql:host=localhost;dbname=mydatabase";
$username = "root";
$password = "";
try {
$conn = new PDO($dsn, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// SQL to create a table
$sql = "CREATE TABLE users (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
$conn->exec($sql);
echo "Table 'users' created successfully";
} catch (PDOException $e) {
echo "Error creating table: " . $e->getMessage();
}
?>
Verify Table Creation
To check if the table was created successfully, use phpMyAdmin
or run the following SQL query:
SHOW TABLES;
To check the table structure:
DESCRIBE users;
Best Practices for Creating MySQL Tables in PHP
✅ Use Prepared Statements – Prevent SQL injection by using prepared statements when inserting or updating data.
✅ Set Primary Keys and Indexes – Always define a primary key (id
with AUTO_INCREMENT
) and use indexes for faster queries.
✅ Use Proper Data Types – Choose data types based on the expected data to optimize performance.
✅ Default Values and Constraints – Use DEFAULT
, NOT NULL
, UNIQUE
, and CHECK
constraints to enforce data integrity.
✅ Use PDO
for Better Security – PDO supports multiple databases and provides better error handling than mysqli
.