Tutorialsplane

PHP Create Database


PHP Create Database:–

Now you’ve understood how to open a connection to the MySQL database server. In this tutorial you will learn how to execute SQL query to create a database.

Before saving or accessing the data, we need to create a database first. The CREATE DATABASE statement is used to create a new database in MySQL.

Creating a Database

After creation of database by using CREATE DATABASE followed by database name ; we apply a mysqli query to check whether database is created or not.In below example you can easily see that how do we create a database named employee.

Example // How to create a database

<?php $servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn-?>connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "CREATE DATABASE employee";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . $conn->error;
}
$conn->close();
?>

PHP Tutorial