PHP Create a MySQL Database

PHP Tutorial

 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.


 Using mysqli (Procedural)


 

Explanation:

  • Connect to MySQL without specifying a database.

  • Use CREATE DATABASE dbname SQL query.

  • Check for success or failure with mysqli_query().


 Using mysqli (Object-Oriented)


 

$conn->query($sql) executes the SQL statement.


 Using PDO


 

PDO::exec() executes SQL statements that don’t return data (like CREATE, UPDATE, DELETE).


 Key Points

  1. You don’t specify a database in the connection when creating one.

  2. Use mysqli_query() or PDO::exec() to execute SQL.

  3. Always check for errors to ensure the database is created.

  4. After creation, you can connect to the new database to create tables.

You may also like...