Skip to main content
connect mysql with PHP

Connect MySQL with PHP step by step

In the previous tutorial, we had done 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.

   Create a new folder in C:/XAMPP/htdocs and name it project1.

   Open http://localhost/phpmyadmin in your browser. Now click on New link on the left sidebar. Here you will find out Create a database, write your database name and click on GO button. That’s it, Your new database is ready.

   Now create a new file inside project folder and name it config.php. Copy the below code and paste it in the config.php file.

<?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"; 
} ?>

   Now open your browser and locate this link http://localhost/project/config.php. If you get a result Database connect successfully. Then you have done it, your database is now connected with PHP.

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.

<?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 “successs message” or it will throw connection error message.

$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 in your circle. If you have any kind query regarding “Connect MySQL With PHP” then please ask question in comment-box. I will give you satisfy answer. Thanks for your love 🙂

admin

HI, i am software developer having knowledge of PHP, C, C++ programming languages. I am very passionate about programming and ability to solve the programming problems. CodeandTuts is my blog where i like to put new tutorial and help new developer.

Leave a Reply

Your email address will not be published. Required fields are marked *