CREATING THE DATABASE

To create a database, connect to MySQL and run the CREATE DATABASE command. This is the
MySQL command to create a database called mydatabase:

CREATE DATABASE ’mydatabase’;


<?php
define(“MYSQLUSER”, “root”);
define(“MYSQLPASS”, “p##V89Te5t”);
define(“HOSTNAME”, “localhost”);
if ($connection = new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS)) {
echo ‘Successful connection to MySQL <br />’;
if ($result = $connection->query(“CREATE DATABASE ’mydatabase’”)) {
$connection->select_db(‘mydatabase’); // use the database
echo “Database created”;
} else {
echo “Problem creating the database. Is the user not allowed
to create database or does the database already exist?”; }
}

?>

Note that the preceding code uses an equal sign in the if statement:
if ($result = $connection->query(“CREATE DATABASE ’mydatabase’”)) {
The way that this statement is processed is that the statement on the right is evaluated fi rst, which
attempts to create the database. That function returns a value, which in this case is TRUE or FALSE.
That value is then assigned to $result, which is then evaluated to determine if the code enclosed by
the if statement should be run.