How to Create MySQL Database using PHP Code

Creating a MySQL database using phpMyAdmin is much easier but sometimes web developers need to create a MySQL database using PHP code. Knowing how to create a MySQL database using PHP is beneficial. This tutorial will give you the exact steps with PHP code to create a MySQL database.

Before going to start, let’s see what you need for this tutorial?

  • You need a Text Editor to write the PHP code (I’ll use Visual Studio Code).
  • Any Browser to run code file like Chrome, Edge, Safari, etc.
  • For this tutorial, we’re using XAMPP, just make sure XAMPP should remain ON and Apache and MySQL both should be running. (Don’t worry, if you’re using a live server, the steps are the same).
  • Also, you should have some knowledge about how to run PHP code.

Now, let’s begin with the main topic.

How to Create MySQL Database using PHP

You just need to do 3 steps to create a database in MySQL using PHP code. Let’s begin with the first step.

Step 1: Create a File with the Name createdb.php in Your Text Editor

To do this, open the text editor and create a new PHP file with the name createdb.php as shown in the image below.

create php file to create MySQL database

In this file, we’ll write our complete PHP code to create a MySQL database. When we’ll run the createdb.php file in our browser, the code will execute and the database will be created.

Now, let’s see the next step.

Step 2: Create a Connection to MySQL Server with PHP

After creating a PHP file, you will create a connection to the MySQL server using PHP code. To create a connection to MySQL server, we use mysqli_connect() function.

mysqli_connect( );

Inside the mysqli_connect() function, we provide the details of the database user like hostname, username, and password in the form of parameters.

Because we’re using localhost XAMPP for this tutorial, our hostname is “localhost“, the username is “root“, and no password (for password use empty double-quotes). As shown in the below code.

//for localhost XAMPP use these details
mysqli_connect("localhost", "root", "");

Let’s see the complete PHP code to create a connection to the MySQL server.

<?php
// database details
$hostname = "localhost";
$username = "root";
$password = "";
// create connection to MySQL server
$con = mysqli_connect($hostname, $username, $password);
// checing connection
if(!$con){
    die("Connection Failed!". mysqli_connect_error());
}
else
{
    echo "You're Successfully Connected to MySQL Server!";
}

Code Explanation:

  • Firstly, I created 3 variables $hostname, $username, and $password to store the user details inside them as a string.
  • Secondly, I created a connection and store it in a variable $con.
  • Lastly, I used if( ) and else statements with the die( ) function to check if the connection is successful or not.

Now, let’s see the last step for creating a MySQL database using PHP.

Step 3: Using SQL and PHP Code to Create MySQL Database

Whenever we create a new MySQL database in PHP, we use CREATE DATABASE command with the database name. Let’s see this step by step.

  • First, use CREATE DATABASE SQL command with the desired name of your database:
// SQL command to create a database
$sql = "CREATE DATABASE webcodzingdb";
  • Then, fire SQL query by using mysqli_query( ) function and store values in the $result variable:
// fire SQL query
$result = mysqli_query($con, $sql);
  • After that, use the if( ) and else statement to check if the database is created or not:
// checking if database is created or not
if($result){
    echo "Database Created Successfully!";
}
else
{
    echo "Database Creation Failed!" . mysqli_error($con);
}
  • At last, use mysqli_close( ) function with $con variable as parameter to close the connection.
// closing connection
mysqli_close($con);

?> 

And don’t forget to put your code inside <?php ... ?> scripts.

Now, put the code together.

See below the complete PHP code to create a MySQL database.

<?php

// User details for Database
$hostname = "localhost";
$username = "root";
$password = "";
// create connection to MySQL server
$con = mysqli_connect($hostname, $username, $password);
// checking connection
if(!$con){
    die("Connection Failed!". mysqli_connect_error());
}
else
{
    echo "You're Successfully Connected to MySQL Server!";
}
// SQL command to create a database
$sql = "CREATE DATABASE webcodzingdb";
// fire SQL query
$result = mysqli_query($con, $sql);
// checking if database is created or not
if($result){
    echo "Database Created Successfully!";
}
else
{
    echo "Database Creation Failed!" . mysqli_error($con);
}
// closing connection
mysqli_close($con);

?>        

Note: You can copy the whole code and paste it inside your createdb.php file to run it.

Now, if you open your browser and run the createdb.php file. Then you will see the PHP code is working and the MySQL database has been created as shown in the image below.

mysql database created successfully

If you run this file again in your browser, then you will see an error of already existing database.

Another way to confirm this is, you can open the phpMyAdmin panel. And you can see there that the database with the name ‘webcodzingdb‘ has been created. As shown in the image below.

database created

All Done! Congratulations!

Now, you know how to create a MySQL database using PHP code.

Conclusion

In this tutorial, you learned how to create a MySQL database in PHP. It’s easy but might be a little bit complicated for beginners. Sometimes, web developers need create databases in PHP. And I hope guide this will be helpful.

If you don’t want to code, then see the following tutorial related to creating databases.

Read Now: How to Create a MySQL Database in phpMyAdmin?

Now, What’s Next? Next, I will recommend you should learn how to connect HTML Form to MySQL Database using PHP to store form entries in the database.

That’s it. If you have any questions related to this topic, then you can leave a comment below. And don’t forget to help others by sharing this tutorial.