Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters in /home/ct33h8azxpia/public_html/wp-content/themes/aquarella-lite/single.php on line 94
How to Create Database Using Mysqli or PDO in PHP? - Experts PHP

How to Create Database Using Mysqli or PDO in PHP?

More than one MySQL databases are also created in PHP.MySQL Database; The ‘CREATE DATABASE database_name’ statement is used to create.

Creat Database using MySQLi

Code :

<?php
$server = "localhost";
$user = "root";
$password = "";
$conn = mysqli_connect($server, $user, $password);
if($conn){
echo "Connected successfully.";
}
else{
echo "Connection failed : ".mysqli_connect_error();
}
$createdb = "CREATE DATABASE expertstutorial";
if (mysqli_query($conn, $createdb)) {
echo "Database Created successfully.";
} else {
echo "database Creation failed : " . mysqli_error($conn);
}
mysqli_close($conn);
?>

Creating Database using PDO

code:

<?php
$server   = 'localhost';
$user     = 'root';
$password = '';
try{
$conn = new PDO("mysql:host=$server;", $user, $password);
echo "Connected successfully.";
$createdb = "CREATE DATABASE expertstutorial";
$conn->exec($createdb);
echo "Database Created successfully";
}
catch(PDOException $e){
echo "Connection failed : " . $e->getMessage();
}
$conn = null;
?>