Connecting MySQL From PHP


Hi,
Although many of you know how to connect MySQL Server which is Running on your machine (localhost) or to any other server from a PHP Code, but I think I can write a few lines for those who does not know how to do it.

If you are very much new to PHP and wondering about how to install php in your Local machine rather than using a server which costs very much and for the beginners it is not worthful, then you may visit http://beg-webdev.blogspot.com/ which can guide you through the installation process. I hope it will help you to install Apache (The web server), MySQL (the Database Server) and PHP (Hypertext Preprocessor) the coding language.
Php is very easy to learn. You can buy a small book from WROX (Beginning PHP,MySQL, Apache Web Development or PHP5 or PHP4) and from there you can learn the basic syntax of PHP (which is very much similar to C).

Now I assume that you know a little bit of php and you have installed Apache, Php and Mysql successfully.
[You need to uncomment the line  ;extension=php_mysql.dll by removing the beginnign ; and restart Apache Web server. If it still throwing errors such as php_mysql.dll not found, then you may need to restart the computer also.]
You can find that you MySQL is running perfectly and accessible from PHP from a piece of code

<?php
//This a function which shows php information
phpinfo();
?>

Save this file as you wish in the Apache s htdocs directory (which is the directory from where you php scripts will run, you can change this also by configuring httpd.conf file in Apcahes conf directory). I prefer to use the name of the file phpinfo.php. Hit this file in the internet explorer like (http://localhost/phpinfo.php) and you will see the informations about installed php on your server. And you can find the MySQL support block from there. If this block is present then you are ready to connect MySQL from php.

So we can start:
1. create a file name conf.php (you can choose other names also)
  put below contents in the file,

 <?php
   $host = “localhost”; //Host where this database is running.
   $user_name = “user”; //Your MySQL Server’s user name
   $password = “pass”; //Your MySQL Server’s  Password
   $db_name = “test”; //Your MySQL Server’s Database.

  //Now connect
  $link = mysql_connect($host,  $user_name, $password);
  if($link)
  {
        echo “Connection to <b>$host</b> successful”;
       //Now you are connected to MySQL, so choose the databse you want to use
       $is_selected = mysql_select_db($db_name)  ;
       //Check the selection
      if($is_selected)
      {
          echo “Database: $db_name selected successfully”;
       }else{
         echo “Error selecting database $db_name;
       }
  }else{
    echo “Error connecting MySQL Server at $host”;
  }

 ?>

Save this script to your servers document root folder (htdocs) and call this script from Internet Explorer or Mozilla.
If you php-mysql configuration is ok then this script will run without any error.

Also for mysql errors you can change this script to 

<?php
   $host = “localhost”; //Host where this database is running.
   $user_name = “user”; //Your MySQL Server’s user name
   $password = “pass”; //Your MySQL Server’s  Password
   $db_name = “test”; //Your MySQL Server’s Database.

  //Now connect
   mysql_connect($host,  $user_name, $password) or die(mysql_error());
  
  //Now you are connected to MySQL, so choose the databse you want to use
   mysql_select_db($db_name)  or die(mysql_error());
    echo “Database: $db_name selected successfully”;
  ?>

 In this case if any errors occures in the runtime, it will automatically notify you with the error details thrown by mysql server.