Database Connection in PHP ?

Database Connection in PHP


Database Connection means connecting your project or code with remote or local Databases like MySQL, etc.


There is a function in PHP that is used to connect with Database but it takes four parameters and these four parameters are required with the sequence.


Function Name -: mysqli_connect();


Four Parameters are -:
  1. hostName
  1. userName of Database
  1. password of Database
  1. database Name


Database Connection in PHP Example(localhost)

  • index.php

    • $hostname = 'localhost';
    • $username = 'root';
    • $password = ''; //if there is no password, if password then enter
    • $databasename = 'test';
    • $con = mysqli_connect($hostname, $username, $password, $databasename);
    • if($con) {echo 'connection is established';}
    • else {echo 'connectionis not established'; }

If your Output -: connection is established that means your connection is done.


If you follow these steps then your database connection is successful but in case if your database connection not done then you can contact me by using a click on sidebar then click contact us and fill the form with appropriate message.



No comments