Home >>MySQL Tutorial >PHP Create MySQLi Database

PHP Create MySQLi Database

Create a MySQL Database Using MySQLi and Object Oriented

<?php
$host = "localhost";
$user = "root";
$pass = "";

// Create database  connection
$con = new mysqli($host, $user, $pass);

// Check  database connection
if ($con->connect_error) 
{
    die("Connection failed: " . $con->connect_error);
} 

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

Create a MySQL Database Using MySQLi and Procedural

<?php
$host = "localhost";
$user = "root";
$pass = "";

// Create database connection
$con =  mysqli_connect($host, $user, $pass);

// Check database connection
if (!$con) 
{
    die("Connection failed: " . mysqli_connect_error() );
} 

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

?>

Note : Here $host(localhost) is servername, $user(root) is default database user name. $pass("") default database password


No Sidebar ads