PHP Create a MySQL Database
🗄️ PHP Create a MySQL Database Tutorial
PHP can be used to create a MySQL database dynamically using the mysqli or PDO extension. This is useful when setting up applications programmatically.
1️⃣ Using mysqli (Procedural)
Explanation:
-
Connect to MySQL without specifying a database.
-
Use
CREATE DATABASE dbnameSQL query. -
Check for success or failure with
mysqli_query().
2️⃣ Using mysqli (Object-Oriented)
✅ $conn->query($sql) executes the SQL statement.
3️⃣ Using PDO
✅ PDO::exec() executes SQL statements that don’t return data (like CREATE, UPDATE, DELETE).
4️⃣ Key Points
-
You don’t specify a database in the connection when creating one.
-
Use
mysqli_query()orPDO::exec()to execute SQL. -
Always check for errors to ensure the database is created.
-
After creation, you can connect to the new database to create tables.
