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

MariaDB creates the database


May 16, 2021 MariaDB


Table of contents


Creating or deleting a database in MariaDB requires permissions that are typically granted only to root users or administrators. U nder these accounts, you have two options to create a database - mysqladmin binary and PHP script.

mysqladmin script

The following example shows how to create a database called Publics using mysqladmin scripts -

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

PHP creates a database script

PHP uses a number of functions when creating mysql_query database. T he function uses two arguments, one optional, and returns the value "true" if successful, otherwise "false".

Grammar

See below Create a database Script syntax -

bool mysql_query( sql, connection );

The parameters are described below -

S.No Parameters and descriptions
1

Sql

This parameter must consist of the SQL query to be performed.

2

connection

When not specified, this optional parameter uses the most recently used connection.

Try the following sample code to create a database -

<html>
   <head>
      <title>Create a MariaDB Database</title>
   </head>

   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'root';
         $dbpass = 'rootpassword';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }

         echo 'Connected successfully<br />';
         $sql = 'CREATE DATABASE PRODUCTS';
         $retval = mysql_query( $sql, $conn );
      
         if(! $retval ) {
            die('Could not create database: ' . mysql_error());
         }

         echo "Database PRODUCTS created successfully
";
         mysql_close($conn);
      ?>
   </body>
</html>

After successful deletion, you will see the following output -

mysql> Database PRODUCTS created successfully 
mysql> SHOW DATABASES; 
+-----------------------+ 
| Database              | 
+-----------------------+ 
| PRODUCTS              | 
+-----------------------+