MySQL – Create Database

MySQL – Create Database

Create Database Using mysqladmin

You would need special privileges to create or to delete a MySQL database. So assuming you have access to the root user, you can create any database using the mysql mysqladmin binary.

Example

Here is a simple example to create a database called TUTORIALS −

[root@host]# mysqladmin -u root -p create mydb
Enter password:

This will create a MySQL database called TUTORIALS.

Create a Database using PHP Script

PHP uses mysql_query function to create or delete a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.

Syntax

bool mysql_query( sql, connection );
Sr.No. Parameter & Description
1 sql

Required - SQL query to create or delete a MySQL database

2 connection

Optional - if not specified, then the last opened connection by mysql_connect will be used.

sql Required - SQL query to create or delete a MySQL database
connection
Optional - if not specified, then the last opened connection by mysql_connect will be used.

Example

The following example to create a database −

<!DOCTYPE html>
<html lang="en">
<head>
<title>Creating MySQL Database</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE mydb";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
MySQL – Connection (Prev Lesson)
(Next Lesson) Drop MySQL Database
', { 'anonymize_ip': true });