Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Php PHP MySQL Connect to a Database

 
 

Share on Google+Share on Google+

PHP MySQL Connect to a Database

Advertisement
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:

     

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.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2009

Related Tutorials

Discuss: PHP MySQL Connect to a Database   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com