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 files 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 but still we don't have any code in it, let's code!

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 the MySQL server, we use the mysqli_connect() function.

→ php code
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.

→ php 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.

→ createdb.php
<?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:
→ php code
// SQL command to create a database
$sql = "CREATE DATABASE webcodzingdb";
  • Then, fire the SQL query by using the mysqli_query( ) function and store values in the $result variable:
→ php code
// 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:
→ php code
// checking if database is created or not
if($result){
    echo "Database Created Successfully!";
}
else
{
    echo "Database Creation Failed!" . mysqli_error($con);
}
  • At last, use the mysqli_close( ) function with the $con variable as a parameter to close the connection.
→ php code
// 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.

→ createdb.php
<?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 in the 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 to 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.