Latest web development tutorials

MySQL Create Database

Create a database using the mysqladmin

Regular user, you may need special permission to create or delete MySQL database.

So here we are logged in as root, the root user has the highest authority, you can use the mysql mysqladmin command to create the database.

Examples

The following command simply demonstrates the creation of the database, the data is named w3big:

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

The above command will create a MySQL database w3big after successful execution.


Create a database using PHP script

PHP use mysql_query function to create or delete MySQL database.

This function has two parameters, in the implementation of successful returns TRUE, otherwise returns FALSE.

grammar

bool mysql_query( sql, connection );
parameter description
sql Required. SQL query to send provisions. Note: The query string should not end with a semicolon.
connection Optional. Provisions of SQL connection identifier. If not specified, the use of an open connection.

Examples

The following example illustrates the use of 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 w3big';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('创建数据库失败: ' . mysql_error());
}
echo "数据库 w3big 创建成功\n";
mysql_close($conn);
?>
</body>
</html>