The CREATE DATABASE statement is used to create a MySQLi database. We must add the CREATE DATABASE statement to the mysqli_query() function to execute the command.
Ex: (Create database)
<?php
$hostName = "localhost";
$userName = "root";
$pass = "";
$database="mydatabase";
// Create database connection
$con = new mysqli($hostName, $userName, $pass);
// Check database connection
if ($con->connect_error)
{
die("Connection failed: " . $con->connect_error);
}
$query = "CREATE DATABASE ".$database;
if ($con->query($query) === TRUE)
{
echo "Database mydatabase created successfully";
}
else
{
echo "Sorry, database creation failed: " . $con->error;
}
?>
Database mydatabase created successfully.
In the above example first, we create a connection with the database then we write a statement to create a database of name my database after that this statement is passed to mysqli_query() along with the connection variable of the database.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning