MySQL Create DB

MySQL Create DB


PHP Create a MySQL Database :

The "CREATE DATABASE db_name" command is used to create a database in MySQL.

To learn more about PHP & MySQL database, we will create a Database. Later we will use it to insert or retrieve data from this database.

Syntax :

         CREATE DATABASE new_db; 

Create Database using PHP and MySQL

In this example first, we will create a database server connection. Then, we can create a new database using the above command/statement.

Example (MySQLi Object-oriented)

Output :

Example (MySQLi Procedural):

output : 

Example (PDO)

Output : 

 

 

Description of the above example for procedural ( same as first and third example )

  • First, we created a database connection using mysqli_connect() function and assign it to $conn object.
  • Then, we assigned a SQL statement in a variable named $sql.
  • After that, we used query() function in connection object to perform database query.
  • If query executed successfully, it will assign some data into $database variable. If the query was not proper, it will save
    false boolean value to the variable.
  • Now, we will print success or failure messages based on $database variable information.
  • close() function closes a previously created database connection.