PHP database connection example program

 INTRODUCTION:                      

Connection is made using a function of php known as                   mysqli_connect("$dbhost","$dbuser","$dbpass","$dbname");

php database connection example program

This function has 4 parameters 
  1. first is your hosting name
  2. second is your user name on hosting
  3. third is password on hosting
  4. forth is database name

PHP CODE for PHP database connection example program 

$dbhost="Localhost";
$dbuser="root";
$dbpass="";
$dbname="database";
     $connect=mysqli_connect("$dbhost","$dbuser","$dbpass","$dbname");
     if(!$connect)
 {
 echo("connection failed");
 }
?>

Explanation of PHP database connection example program

hostname 

It is the name of your server in this code my server is Localhost as I am coding using Xampp.
On the live server, you should have to use your server name.

User

If your username for the server in Localhost my username is root as it is default user for Xampp.
On the live server, you should have to use your username.

Pass

It is the password on the server I have no password. so it is empty.
On the live server, you should have to use your password.

Dpname

It is the name of your database in the server.
On the live server, you should have to use your database name.
Previous Lecture                                                                                                                   Next Lecture