The database has one or more tables.

You need CREATE permission to create or delete MySQL database.


Creating a MySQL database using MySQLi and PDO

The CREATE DATABASE statement is used to create a database in MySQL.

In the following example, a database named "myDB" is created:

Instance (MySQLi - Object-oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
// 创建连接
$conn = 
new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
// 创建数据库
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
    
echo "数据库创建成功";
} else {
    echo 
"Error creating database: " . $conn->error;
}
$conn->close();
?>

Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
// 创建连接
$conn = mysqli_connect($servername, 
$username, $password);
// 检测连接
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
// 创建数据库
$sql = "CREATE 
DATABASE myDB";
if (mysqli_query($conn, $sql)) {
    echo "数据库创建成功";
} else {
    echo "Error 
creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

Note: The following uses PDO instance to create database "myDBPDO":

Instance

Use PDO:

<?php
$servername
= "localhost";
$username = "username";
$password = " password";

try {
$conn = new PDO( "mysql:host=$servername;dbname=myDB", $username, $password);

// Set PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE myDBPDO";

// Use exec() because no result is returned
$ conn->exec($sql);

echo
"Database created successfully<br>";
}
catch(
PDOException $e)
{
echo
$sql . "<br>" . $e-> ;getMessage();
}

$conn = null;
?>

## Tips: The biggest benefit of using PDO is to query in the database When there is a problem in the process, you can use the exception class to solving issues. If an exception occurs in the try{ } code block, the script will stop execution and jump to the first catch(){ } code block to execute the code. In the code block captured above we output the SQL statement and generate the error message.

Related video tutorial recommendations: "mysql tutorial"//m.sbmmt.com/course/list/51.html


Hot AI Tools
Undress AI Tool
Undress AI Tool

Undress images for free

AI Clothes Remover
AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress
Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT
ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT
Stock Market GPT

AI powered investment research for smarter decisions

Popular tool
Notepad++7.3.1
Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version
SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1
Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6
Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version
SublimeText3 Mac version

God-level code editing software (SublimeText3)

Note##Note: When you create a new database, you must specify three parameters for the mysqli object ( servername, username and password).

Tip: If you use another port (default is 3306), add an empty string for the database parameter, such as: new mysqli("localhost", "username", "password", "", port)