PHP MySQL creates database

The database has one or more tables.

Create database

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

Syntax

CREATE DATABASE database_name

Category Detailed explanation
Basic syntaxcreate database database name;
Examplecreate database liwenkai;
Example description Create a database, the name of the database is liwenkai

In order for PHP to execute the above statement, we must use the mysql_query() function. This function is used to send a query or command to the MySQL connection.


Rules for creating a database:

1. Cannot be used with other databases Duplicate name, otherwise an error will occur.

2. The name can be composed of any letters, Arabic numerals, underscores (_), and "$". It can start with any of the above characters, but it cannot use numbers alone, otherwise it will be confused with the numerical value.

3. You cannot use the MYSQL keyword as the database name or table name.

4. By default, the database name under Windows is not sensitive. The opposite is true under Linux, so in order to facilitate database transplantation between platforms, it is recommended to use lowercase to define database names and table names.


Example

A database named "myDB" is created in the following example:

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 $servername = "localhost";
 $username = "root";
 $password = "root";
 
 
 // 创建连接
 $conn = mysqli_connect($servername, $username, $password);
 // 检测连接
 if (!$conn) {
     die("连接失败: " . mysqli_connect_error());
 }
 // 创建数据库
 $sql = "CREATE DATABASE my_db";
 if (mysqli_query($conn, $sql)) {
     echo "数据库创建成功";
 } else {
     echo "数据库创建失败: " . mysqli_error($conn);
 }
 mysqli_close($conn);
 ?>

Program running result:

Database created successfully


Delete database

## Basic syntax## Exampledrop database liwenkai; Example descriptionDelete a database, the name of the database is liwenkai

Note:
drop is Chinese and can be translated as falling down, not wanting anymore

database It is refers to the library

# 是 The name of the library refers Example


steer in the example below deletes a database called "MyDB":

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 $servername = "localhost";
 $username = "root";
 $password = "root";
 
 
 // 创建连接
 $conn = mysqli_connect($servername, $username, $password);
 // 检测连接
 if (!$conn) {
     die("连接失败: " . mysqli_connect_error());
 }
 // 创建数据库
 $sql = "drop database  myDB";
 if (mysqli_query($conn, $sql)) {
     echo "数据库删除成功";
 } else {
     echo "数据库删除失败: " . mysqli_error($conn);
 }
 mysqli_close($conn);
 ?>
Program run results:

#Database deletion is successful

# [Remember] Note

: After the database is deleted, all the following data will be deleted, so you must be careful and corresponding before deleting Backup.

Continuing Learning
||
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $servername = "localhost"; $username = "root"; $password = "root"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); // 检测连接 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } // 创建数据库 $sql = "CREATE DATABASE my_db"; if (mysqli_query($conn, $sql)) { echo "数据库创建成功"; } else { echo "数据库创建失败: " . mysqli_error($conn); } mysqli_close($conn); ?>
submitReset Code
## Category Detailed explanation
drop database library name;