0% found this document useful (0 votes)
8 views

Create MySQL Database at the Localhost

Uploaded by

Legesse Samuel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Create MySQL Database at the Localhost

Uploaded by

Legesse Samuel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Create MySQL Database at the

Localhost
First, let me tell you what PHPMyAdmin is. It is a control panel from
where you can manage your database that you have created. Open
your browser and go to localhost/PHPMyAdmin or click “Admin” in
XAMPP UI.

When you first installed XAMPP, it only created the username for it to
be accessed, you now have to add a password to it by yourself. For
this, you have to go to User account where the user is same as the
one shown in this picture:

Now click Edit privileges and go to Change Admin password, type


your password there and save it. Remember this password as it will
be use to connect to your Database.
Note: It is not necessary to change password to access databases on
local host. It is a good practice and that is why we have used a
password.

Create Database
Now return to the homepage of phpmyadmin. Click New button to
create a new database.

In the new window, name your database as per your need, I am


naming it “practice”. Now select Collation as utf8_general_ci, as we
are using it for learning purposes and it will handle all of our queries
and data that will be covered in this tutorial series. Now click
on Create and your database will be created.
The newly created database will be empty now, as there are no tables
in it. I will be covering that in the upcoming series where we will learn
how to create tables and insert data in it. In this tutorial, we are going
to connect this database to a localhost using PHP.

Create a Folder in htdocs


Now, locate the folder where you installed XAMPP and open htdocs
folder (usually c:/xampp). Create a new folder inside c:/xampp/htdocs/
and name it “practice” we will place web files in this folder. Why we
have created folder in htdocs? XAMPP uses folders in htdocs to
execute and run your PHP sites.

Note: If your using WAMP, then add your practice folder in


c:/wamp/www folder.

Create Database Connection File In PHP


Create a new php file and name it db_connnection.php and save it.
Why am I creating a separate database connection file? Because if
you have created multiple files in which you want to insert data or
select data from the databases, you don’t need to write the code for
database connection every time. You just have to include it by using
PHP custom function include (include ‘connection.php’) on the top of
your code and call its function and use it. It also helps when you are
moving your project location from one PC to another and you have to
change the values on the single file and all the changes will be applied
to all the other files automatically. Write the following code in your
db_connection file.

1. <?php
2.
3. function OpenCon()
4. {
5. $dbhost = "localhost";
6. $dbuser = "root";
7. $dbpass = "1234";
8. $db = "example";
9.
10.
11. $conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn ->
error);
12.
13.
14. return $conn;
15. }
16.
17. function CloseCon($conn)
18. {
19. $conn -> close();
20. }
21.
22. ?>

Here is the explanation of the variable that we have used in our


db_connection file:

1. $dbhost will be the host where your server is running it is usually


localhost.
2. $dbuser will be the username i.e. root and $dbpass will be the
password which is the same that you used to access your phpmyadmin.
3. $dbname will be the name of your database which we have created in
this tutorial.
Create new php file to check your database
connection
Create a new php file to connect to your database. Name it index.php
and add this code in this file.

1. <?php
2. include 'db_connection.php';
3.
4. $conn = OpenCon();
5.
6. echo "Connected Successfully";
7.
8. CloseCon($conn);
9.
10. ?>

Run it!
Now open your browser and goto localhost/practice/index.php and you
should see this screen:

Confirmation Message

Congratulations! You’ve successfully connected your database with


your localhost! If you are not able to see this screen, then check if you
have done everything right in your db_connection.php file.

Download Your RestAPI Ebook Now


Simple enter your email address and get the download link in your Inbox.

I agree to the Cloudways Terms of Service & Privacy Policy


GET IT NOW

Create MySQL Database at


Cloudways Server

You might also like