0% found this document useful (0 votes)
19 views9 pages

Creating and Dropping Tables in Mysql Using PHP

Uploaded by

sanjaypcdoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Creating and Dropping Tables in Mysql Using PHP

Uploaded by

sanjaypcdoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CREATING AND

DROPPING TABLES IN
MYSQL USING PHP
Introduction

Objective: Understand how to create and drop tables


in MySQL using PHP.
Key Concepts:
SQL commands in PHP
Connection and error handling
Creating a MySQL Table
Syntax:
sql

CREATE TABLE table_name (


column1 datatype constraints,
column2 datatype constraints,
...
);
Example:
Creating a "Users" table with columns for ID,
username, and email.
Creating a Table in PHP
To create a table, use the CREATE TABLE SQL command inside a PHP script.
Example: Creating a users table with columns for id, username, and email.
php

$sql = "CREATE TABLE users (


id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL
)";

if ($conn->query($sql) === TRUE) {


echo "Table 'users' created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
Dropping a MySQL Table

Syntax:
sql
DROP TABLE table_name;
Purpose: Deletes the table and all its data.
Dropping a table removes it permanently, along with all its data.
Use the DROP TABLE SQL command within PHP to delete a table.
php
$sql = "DROP TABLE users";

if ($conn->query($sql) === TRUE) {


echo "Table 'users' dropped successfully";
} else {
echo "Error dropping table: " . $conn->error;
}
Error Handling

Always check for connection errors.


Use try-catch blocks in larger applications to handle exceptions.
Example:
php
Copy code
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
CONCLUSION
Recap:
Creating tables helps store structured data.
Dropping tables removes tables and their data
permanently.
Best Practices:
Test SQL commands carefully.
Avoid dropping tables unless necessary.
THANK YOU
DAVID 22-UCS-023

You might also like