How To Connect MySQL with PHP [Step By Step]

connect mysql with PHP

In the previous tutorial, we made practice of 30+ core PHP examples and tutorials for beginner tutorial. Now in this tutorial I will show you how to connect MySQL database with PHP. MySQL is simple and open-source (free) database.

If you have installed XAMPP web server then it will comes up with phpMyAdmin tool. phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySQL over the Web. phpMyAdmin supports a wide range of operations on MySQL and MariaDB.

Frequently used operations (managing databases, tables, columns, relations, indexes, users, permissions, etc) can be performed via the user interface, while you still have the ability to directly execute any SQL statement.

Step-1 : Start Apache and MySQL in XAMPP server

Run XAMPP server and start Apache and MySQL in it as shown below.

start-apache-mysql-in-xampp

Step-2: Create New Database In PhpMyAdmin

Open http://localhost/phpmyadmin in your browser.  Create a test database as shown below.

Step-3 : Create Connection File

Create a new file in C:/XAMPP/htdocs/project and name it config.php file. If you dont have project folder in htdocs folder, please create it.

config.php

<?php 
$db_hostname = 'localhost'; // Database hostname 
$db_username = 'root'; // Database username 
$db_password = ''; // Database password 
$db_name = 'test'; // Database name 
$conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_name); 
if(!$conn) { 
    echo "Unable to connect database".mysqli_error($conn);die; 
} else { 
    echo "Database connected successfully"; 
} ?>

Next, navigate this url http://localhost/project/config.php in your browser. If you get a result Database connect successfully. Then you have done it, your database is now connected with PHP.

Related:  Laravel 5.8 - Setup Yajra Datatable with AdminLTE [Step By Step]

Let’s see what we did in the code step by step. The first four lines are details of your database. It means your database hostname (If you are working on local server like XAMPP, then it is “localhost”),  your database username (default username “root”),  your password (default password is “”). In the last line you have to write your database name which you want to connect. Here we are using test database.

<?php 

$db_hostname = 'localhost'; // Database hostname 
$db_username = 'root'; // Database username 
$db_password = ''; // Database password 
$db_name = 'test'; // Database name 

?>

In the next line we use mysqli_connect function which will connect your database with PHP. If your database connect successfully then it will show success message or it will throw connection error message. izlenme satın al instagram

$conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_name);

if(!$conn) 
{
    echo "Unable to connect database".mysqli_error($conn);die;
}
else
{
    echo "Database connected successfully";
}

I hope that you enjoyed the this article. If you like this article then please share it with your friends. If you have any query regarding how to connect MySQL with PHP then please ask question in comment-box. I will help you to resolve it. Thanks for your love 🙂

About Chintan Panchal

I am web developer having 6+ year of experience of web development. Currently working on laravel and have worked on codeigniter, cakephp, symfony. Thank you :)

View all posts by Chintan Panchal →

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.