Database connection is important feature when you start developing a Dynamic website, on which you want user to register / login into your web application, so before allowing these user to login you need to store their information in your Database. And today am going to show you on how to make a database connection in your PHP project.

First of all you need to have a local server like XAMMP or WAMMP installed on your computer and make sure the server is turned on. This local server will allow your web application to communicate.

After turned on your XAMMP or WAMMP, open PHPMYADMIN or you can type on your browser localhost/phpmyadmin in the address bar. and the screen will open.


When this screen popup, now go to phpmyadmin and creating your database like students
So, now we have a database called students and we want to connect this database to our application.
Open your code editor and create a file called config.php and write the following codes.

<?php
$db_name = "student";
$username = "root";
$password = "";
$servename = "locaclhost";
$conn = mysqli_connect($servename ,$username ,$password ,$db_name );
if(!$conn){
die("No connection to database");
} else{
echo "Successfully connected to database";
}
?>