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 dbname SQL 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

  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.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *