PHP - Connect to MySql server

php provides you with two main ways to connect to mysql databases:

  • mysqli(mysql improved) : is a relational database driver used in the php to provide an interface with mysql databases. mysqli is an improved version of the older php mysql driver, offering various benefits. it features both procedural (function-oriented) and object-oriented interfaces. however, if you know you're only ever going to work with mysql, and you want to squeeze the most out of mysql's power from your php scripts, then mysqli is a good choice.
  • pdo(php data object) : this is an object-oriented extension that sits between the mysql server and the php engine. you can use the same extension to talk to lots of other database systems like(mysql, postgresql, oracle).

for installation details, go to :http://www.php.net/manual/en/mysqli.installation.php

for installation datails, go to :http://www.php.net/manual/en/pdo.installation.php


php - create a connection to the mysql server

the php mysqli_connect() function is used to open a new connection to mysql server.

before using database we must open a connection to the mysql server.

this method returns an object which represents the connection to a mysql server.

syntax:

          mysqli_connect(host, username, password, databasename, port, socket)
parameterdescription
hostspecifies a host name or an ip address. it is optional.
usernamespecifies the mysql user name. it is optional.
passwordspecifies the mysql password. it is optional.
dbnamespecifies the default mysql database name to be used. it is optional.
portspecifies the port number to attempt to connect to the mysql server. optional
socketspecifies the socket or named pipe that should be used. it is optional.
 <?php
    
          $host = "localhost";
               
          $username = "steve";
               
          $password = "12steve";
               
          $dbname = "images";               
               
     // mysqli procedural style use to connect mysql database server
                
   $object = mysqli_connect( $host, $username, $password, $dbname);

  if( $object ){     
                     echo  "able to connect to database";
                  
                 }else {              
     
  echo  "unable  to connect to database what error: ". mysqli_connect_error(); 
  
                   }
         
                     
   $object = mysql_connect( $host, $username, $password, $dbname ); //old way  

/*this method is deprecated as of php 5.5.0 and will be removed in the future.
instead, the mysqli should be used.*/
 
  ?>

php - closing a database connection

the mysqli_close() function can be used to close a connection to a database.

this method return true on success or false on failure.

syntax:

         mysqli_close( $object )
 <?php
    
          $host = "localhost";
               
          $username = "steve";
               
          $password = "12steve";
               
          $dbname = "images";               
               
     // mysqli procedural style use to connect mysql database server
                
   $object = mysqli_connect( $host, $username, $password, $dbname);

  if( $object ){     
                     echo  "able to connect to database";
                  
                 }else {              
     
  echo  "unable  to connect to database what error: ". mysqli_connect_error(); 
  
                   }
                      // close the database connection
                      
                      mysqli_close( $object );
          
  ?>