MySQLi - Create Database


Create Database using mysqladmin

You would need special privileges to create or to delete a MySQLi database. So assuming you have access to root user, you can create any database using mysql mysqladmin binary.


Here is a simple example to create database called TUTORIALS

[root@host]# mysqladmin -u root -p create TUTORIALS
Enter password:******

This will create a MySQLi database TUTORIALS.

Create Database using PHP Script

PHP uses mysqli_query function to create or delete a MySQLi database. This function takes two parameters and returns TRUE on success or FALSE on failure.


Try out the following example to create a database −

      <title>Connecting MySQLi Server</title>
         $dbhost = 'localhost:3306';
         $dbuser = 'root';
         $dbpass = '<password here>';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
         if(! $conn ){
            echo 'Connected failure<br>';
         echo 'Connected successfully<br>';
         if (mysqli_query($conn, $sql)) {
            echo "Database created successfully";
         } else {
            echo "Error creating database: " . mysqli_error($conn);

If connected and created database successfully,then the sample output should be like this −

Connected successfully
Database created successfully