PHP Create a MySQL Database

To create and delete a database you should have admin privilege. Its very easy to create a new MySQL database. PHP uses mysql_query function to create a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.You will need special CREATE privileges to create or to delete a MySQL database.

The CREATE DATABASE statement is used to create a database in MySQL.

Example (MySQLi Object-oriented) -

Example (MySQLi Procedural) -

Example (PDO) -

Selecting a Database

Once you establish a connection with a database server then it is required to select a particular database where your all the tables are associated. This is required because there may be multiple databases residing on a single server and you can do work with a single database at a time.

PHP provides function mysql_select_db to select a database.It returns TRUE on success or FALSE on failure.

Example -