Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MySQL creates a database


May 15, 2021 MySQL


Table of contents


MySQL creates a database


Create a database using mysqladmin

With normal users, you may need specific permissions to create or delete mySQL databases.

So this side uses root user login, root user has the highest permissions, can use mysql mysqladmin command to create a database.

Instance

The following command simply demonstrates the process of creating a database called W3CSCHOOL:

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

The MySQL database W3CSCHOOL is created after the above command is executed successfully.


Use PHP scripts to create a database

PHP uses mysql_query function to create or delete mySQL databases.

The function has two arguments that return TRUE if executed successfully, otherwise FALSE is returned.

Grammar

bool mysql_query( sql, connection );
Parameters Describe
Sql Necessary. S pecify the SQL query to send. Note: The query string should not end with a sign.
connection Optional. S pecifies the SQL connection identifier. If not specified, the last open connection is used.

Instance

The following example demonstrates using PHP to create a database:

<html>
<head>
<meta charset="utf-8"> 
<title>创建 MySQL 数据库</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('连接错误: ' . mysql_error());
}
echo '连接成功<br />';
$sql = 'CREATE DATABASE W3CSCHOOL';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('创建数据库失败: ' . mysql_error());
}
echo "数据库 W3CSCHOOL 创建成功\n";
mysql_close($conn);
?>
</body>
</html>