The following code example demonstrates Creating a Database with MySQLi.
As can be seen in the example, here we use the Object Oriented MySQLi to create a database. Therefore, first we create three variables representing the hostname, database user, and the password. After that, we create an object of the MySQLi class by passing these three variables as the parameters to the constructor. Once, we create a connection with the MySQL server successfully, create another string variable containing the ‘CREATE DATABASE ‘ command. Finally, we call the query() method using the connection object and the command string as the parameter that creates the database.
<?php
$server_host="localhost";
$database_user="root";
$password="";
// Connect with MySQL Server
$con1=new mysqli($server_host, $database_user, $password);
// Create a query for database creation
$query="create database Institute";
// Execute the query
$b=$con1->query($query);
if($b)
{
echo 'Database Created Successfully!';
}
else
{
echo 'Database could not be created due to error: <br>',$con1->error;
}
// close the connection
$con1->close();
?>
Output
After that, you can open phpMyAdmin and find that the database Institute has been created.
Further Reading
Examples of Array Functions in PHP