How to create a Database in PHP/MySQL

You can create a database in php using two methods: First one is using phpmyadmin and the second one is from php code.

How to create a Database in PHP/MySQL
How to create a Database in PHP/MySQL

Using php for websites can be very helpful when creating dynamic content websites. To start using PHP with MySQL, the first step is to create a Database and then populate it with some data.

In this guide, you will learn to create a MySQL database in two ways, both of which are quite easy and fast to do.

Create MySQL database using phpmyadmin

The easy way to create a database in MySQL is via phpmyadmin. If you have access to cpanel or hosting, you can easily follow these simple steps to create a new database.

  1. Go to your Hosting Admin Panel
  2. Navigate to CPanel
  3. Look for MySQL Databases and click on it
  4. Click Create New Database
  5. Enter the Name of the database [Keep it simple and in lowercase]
  6. Click Create
Create MySQL Database using cpanel
Cpanel Dashboard from where you can control the entire website

By doing these steps your MySQL database has been created, now we have to assign a user to it so we can have a username, password, and database name for our PHP connections.

Create Database using phpmyadmin
Create a Database using phpmyadmin

Create a New User for the MySQL Database

  1. Create New User
  2. Add Username and Password
  3. Click Create User
Create New User in MySQL Database
Create a New User in the MySQL Database
⚠️ On Local Host, you don’t have to set username or password

After creating a user for our database, the next step is to assign this user to the database. You can create multiple users for the same database but it is not recommended as all the users assigned will have access to the database.

Assign a User to MySQL Database

  1. Go to assign a user to the database
  2. Select Database from Database dropdown
  3. Select User from User dropdown
  4. Click Assign Button

Create MySQL Database using a PHP file

To create a database using a PHP file, we need to run a MySQL query. It can be executed directly into phpmyadmin as well as in a php file. Follow these steps to create a php file that can create a new database.

  1. Go to File Manager
  2. Create a new php file [Give it a Name of your choice]
  3. Write the following code in it.
//Server Name is Mostly Localhost so don't change it
$servername = "localhost";
//This is the usernameof your server
$username = "username";
//This is the password of your server
$password = "password";

// Create database [myDB] is the name of database
if (mysqli_query($conn, $sql)) {
  echo "Database created successfully";
} else {
  echo "Error creating database: " . mysqli_error($conn);
//Closing connection

After pasting the above code, click save and close. Now we have to execute this file so the php code can create the database.

To run this file, just type the following address in your search bar

Replace with your website link and filename.php with your created php file and hit enter. After executing this file, nothing will appear on your screen, but the database will be created.


Creating a Database is an easy step while developing dynamic websites. But while designing a database, you have to keep several things in mind like website requirements, number of tables, and their column names, etc.

So we have listed both methods to create a MySQL database. If you face any problems, you can suggest us an edit in the comment section and we will update this article.

Umair Javed is a Professional WordPress Developer with 5 Years of experience. He works on multiple tech stacks like core PHP, Laravel, WordPress, and Shopify. Moreover, he is a passionate technical writer and writes article on development topics and problem solutions.