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.
- Go to your Hosting Admin Panel
- Navigate to CPanel
- Look for MySQL Databases and click on it
- Click Create New Database
- Enter the Name of the database [Keep it simple and in lowercase]
- Click Create
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 a New User for the MySQL Database
- Create New User
- Add Username and Password
- Click Create User
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
- Go to assign a user to the database
- Select Database from Database dropdown
- Select User from User dropdown
- 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.
- Go to File Manager
- Create a new php file [Give it a Name of your choice]
- Write the following code in it.
<?php
//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
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
//Closing connection
mysqli_close($conn);
?>
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
[https://mywebsite.com/filename.php]
Replace mywebsite.com 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.
Conclusion
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.