Connect MYSQL Database using PHP

Hey Guys,
In this PHP tutorial, you are going to learn about how to connect MySQL Database to our program using Hypertext Preprocessor.

Connect MySQL Database to our program using PHP

MySQL is an Oracle-backed open source relational database management system based on Structured Query Language (SQL). MySQL can run on virtually all platforms, including Linux, UNIX and Windows. MySQL is used in a wide range of applications and is the most commonly used database among others.

Connecting a database is a very first task when we have to perform operations on Database. We can connect to a database simply by using  mysqli_connect( ) method.

Real life applications of connecting a database to our program:

Whenever we require to store the data we need a database to store the user data. In such cases, we create a database and save our data there. To do so we have to first connect our database with the program using the below mentioned command.


In case of signup form, we have to save the user data to do so a database is required.


We have to pass 4 parameters to the mysqli_connect( ) method that are .

  1. Server Name: Name of the server on which we are working.
  2. User Name: Name of the user you can find that in XAMPP settings.
  3. Password: Password to the user.
  4. Database Name: Name of the Database you are working on.
Syntax :
Now, I have given a small example to show how to connect to a database.



$servername = "localhost";

$username = "root";

$password = "";

$dbname = "project";

$q = mysqli_connect($servername,$username,$password,$dbname);

echo  "___database connected___";


  die("database not connected".mysqli_connect_error());



  • Here, I used localhost as my server.
  •  Also, I used root as my user.
  • Here, the project is my database name.
  • Used a simple if statement to show whether Database is connected or not.


 ___database connected___

You can also read:

Leave a Reply

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