PHP MySQL Connect to a Database

In WAMP software stack, php and mysql are bundled together and we can use it with so much ease.

PHP MySQL Connect to a Database

PHP MySQL Connect to a Database:

     

In WAMP software stack, php and mysql are bundled together and we can use it with so much ease. To connect with mysql server we can use two ways one is phpMyAdmin and another is simple manually hand written code.

We will demonstrate the second option in this tutorial

The first step of any kind of connectivity is to check whether the connectivity has been established or not. To check the connectivity we need to know three things: The host name, Username, and respective Password.

Usually Host name is localhost, Username is root and password is " " (blank, unless you have changed the password at the time of installlation). By mentioning all these details  we can connect a PHP page with MySQL server.

Following piece of code will help you to check whether your PHP web page has been connected with MySQL server or not

<?php

$dbc=@mysqli_connect('localhost','root','','roseindia')

or die("error");

mysqli_close($dbc);

?>

In the above coding you can see a special operator called Error Control Operator '@', this operator is completely optional, this operator is useful at the time when connection could not be established, if you use this operator at the time when connection could not be made, you will get following output:

error

If you don't use '@' symbol the output will be as follows:


Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'basu'@'localhost' (using password: NO) in C:\wamp\www\php\dbConn.php on line 2
error

NOTE: In the password section (right after user) do not put any space in between the single quote otherwise an error message would be generate, instead of valid user name.