To create a database, run the create command:

CREATE DATABASE Specifies the DATABASE name.Copy the code

The following command is a simple example of creating a database named RUNOOB:

[root@host]# mysql -u root -p Enter password:******Copy the code

Create the database using mysqladmin

As a normal user, you may need specific permissions to create or delete MySQL databases.

So we use root user login, root user has the highest permission, can use mysql mysqladmin command to create the database.

The following command is a simple example of creating a database named RUNOOB:

[root@host]# mysqladmin -u root -p create RUNOOB
Enter password:******
Copy the code

After the preceding command is executed successfully, the MySQL database RUNOOB is created.


Use PHP scripts to create databases

PHP uses the mysqli_query function to create or delete MySQL databases.

This function takes two arguments and returns TRUE on success and FALSE otherwise.

grammar

mysqli_query(connection,query,resultmode);
Copy the code
parameter describe
connection A necessity. Specify the MySQL connection to use.
query Required, specifies the query string.
resultmode Optional. A constant. Can be any of the following values: * MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data)
  • | MYSQLI_STORE_RESULT (the default)

The instance

The following example demonstrates creating a database using PHP:

Creating a database

<? php $dbhost = ‘localhost’; $dbuser = ‘root’; $dbpass = ‘123456’; $conn = mysqli_connect($dbuser, $dbpass); if(! $conn) {die(‘ connection error: ‘.mysqli_error ($conn)); } echo ‘connect successfully <br />’ $sql = ‘CREATE DATABASE RUNOOB’; $retval = mysqli_query($conn,$sql ); if(! $retval) {die(‘ failed to create database: ‘.mysqli_error ($conn)); } echo “RUNOOB created successfully \n”; mysqli_close($conn); ? >