Skip to main content
connect mysql with PHP

Connect MySQL with PHP step by step

In the previous tutorial, we saw that 30+ core PHP examples and tutorials for beginner for practicing. Now in this tutorial I will show you how to connect MYSQL with PHP database. Here you notice that why I use MYSQL database and not other database. The reason is MYSQL is simple and open-source database. One other most important feature is PHPMYADMIN tool. PHPMYADMIN is a tool through which you can handle MYSQL database easily. You can perform various actions like to create a new database, delete a database, create table, insert table, import DB, export DB, etc. Therefore I choose a MYSQL database with PHP. Let’s perform the tutorial.

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

   Open “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. We will learn more about PHPMYADMIN in another tutorial.

   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 “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, WAMP, MAMP 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. If you have any kind problem regarding “Connect MySQL With PHP” then please ask question in comment-box. I will come with another interesting web-development article. 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 *