Make a SQL query using the CREATE DATABASE statement, after that we will execute this SQL query through passing it to the PHP mysqli_query() function to finally create our database. The following example creates a database named demo.

Example : MySQLi Procedural

<?php

    // Create connection
    $link = mysqli_connect($servername, $username, $password);
    // Check connection
    if (!$link) {
      die("Connection failed: " . mysqli_connect_error());
    }

    // Create database
    $sql = "CREATE DATABASE demo";
    if (mysqli_query($link, $sql)) {
      echo "Database created successfully";
    } else {
      echo "Error creating database: " . mysqli_error($link);
    }

?>

Example : MySQLi PDO

<?php

    try {
      $link = new PDO("mysql:host=$servername", $username, $password);
      // set the PDO error mode to exception
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "CREATE DATABASE demo";
      // use exec() because no results are returned
      $link->exec($sql);
      echo "Database created successfully<br>";
    } catch(PDOException $e) {
      echo $sql . "<br>" . $e->getMessage();
    }

?>

Example : MySQLi Object Oriented

<?php

    // Create connection
    $link = new mysqli($servername, $username, $password);
    // Check connection
    if ($link->connect_error) {
      die("Connection failed: " . $link->connect_error);
    }

    // Create database
    $sql = "CREATE DATABASE myDB";
    if ($link->query($sql) === TRUE) {
      echo "Database created successfully";
    } else {
      echo "Error creating database: " . $link->error;
    }

?>