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 syntax | create database database name; |
Example | create 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