Create a MySQLi Database

A database is used to store the information in the form of rows and columns. The database stores data in tables, rows, and columns form to quickly served the asked queries.

The PHP mysqli_query function allows you to create or delete the MySQLi database. The mysqli_query function takes two parameters first is the connection, and the second is the query. This function returns TRUE on success and returns FALSE on failure.

Example:-
The following PHP code will create a Database.

<pre class="preetyprint">
<html>
   <head>
      <title>Creating MySQLi Database</title>
   </head>
   
   <body>
      <?php
         $host = 'localhost:3306';
         $user = 'root';
         $password = '<password here>';
         $conn = mysqli_connect($host, $user, $password);
   
         if(! $conn ){
            echo 'Connection failure<br>';
         }
         echo 'Connection sucssfully Established<br>';
         $sql = "CREATE DATABASE Student";
         
         if (mysqli_query($conn, $sql)) {
            echo " Student Database created";
         } else {
            echo "Error creating database: " . mysqli_error($conn);
         }
         mysqli_close($conn);
      ?>
   </body>
</html>
</pre>

If the query is executed scussefully, then the following output will be printed:-

Connected successfully
Student Database created

Leave a Reply

Your email address will not be published. Required fields are marked *