PHP & MySQL: How to create/alter/drop tables

Updated: January 12, 2024 By: Guest Contributor Post a comment

Overview

In today’s web development scenario, managing databases is a crucial skill. MySQL is one of the most popular relational database management systems, and PHP is a server-side scripting language that is widely used for web development. Often, these two are combined to manage the backend data of a web application effectively. In this tutorial, we will go through the essential operations of creating, altering, and dropping tables in a MySQL database using PHP.

What Will You Learn?

  • How to set up a PHP script to connect to MySQL
  • Creating MySQL tables utilizing PHP
  • Altering tables to fit evolving data requirements
  • Dropping tables that are no longer necessary
  • Best Practices in PHP and MySQL

Prerequisites

Before we start, make sure you have a PHP environment setup, access to a MySQL database, and a basic understanding of SQL and PHP.

Setting Up a Database Connection

Our first step is to establish a connection with the MySQL database:

<?php
$servername = 'localhost';
$username = 'your_username';
$password = 'your_password';
$dbname = 'your_dbname';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die('Connection failed: ' . $conn->connect_error);
}
echo 'Connected successfully';
?>

Creating Tables

Once connected, we can start creating tables. Here’s a simple example:

<?php
$sql = "CREATE TABLE users (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  firstname VARCHAR(30) NOT NULL,
  lastname VARCHAR(30) NOT NULL,
  email VARCHAR(50),
  reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
  echo "Table users created successfully";
} else {
  echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

This script will create a table named ‘users’ with columns for user ID, first name, last name, email address, and registration date.

Altering Tables

To change the structure of an existing table:

<?php
$sql = "ALTER TABLE users
ADD birthday DATE NOT NULL";

if ($conn->query($sql) === TRUE) {
  echo "Table altered successfully";
} else {
  echo "Error altering table: " . $conn->error;
}

$conn->close();
?>

This will add a new column ‘birthday’ of type DATE to the ‘users’ table.

Dropping Tables

If you want to remove a table completely, use the DROP TABLE command:

<?php
$sql = "DROP TABLE if exists users";

if ($conn->query($sql) === TRUE) {
  echo "Table dropped successfully";
} else {
  echo "Error dropping table: " . $conn->error;
}

$conn->close();
?>

Best Practices

  • Regularly back up your database before performing operations that alter its structure.
  • Ensure to sanitize and validate user input to safeguard against SQL injection.
  • Use mysqli_real_escape_string() or prepared statements for better security.
  • Close your database connections to free up resources.

Conclusion

Creating, altering, and dropping tables are fundamental operations that you’ll perform while managing a MySQL database with PHP. With the knowledge you’ve gained from this tutorial, these tasks should become routine components of your web development work.