How to connect database in php using mysql , mysqli and pdo
By

Joon Corporation

you must know how to establish a connection to MySQL from inside a PHP script. To perform basic queries from within MySQL is very easy. This article will show you how to get up and running. Lets get started. The first thing to do is connect to the database.The function to connect to MySQL is called mysql_connect. This function returns a resource which is a pointer to the database connection. It is also called a database handle.
database

you must know how to establish a connection to MySQL from inside a PHP script. To perform basic queries from within MySQL is very easy. This article will show you how to get up and running. Lets get started. The first thing to do is connect to the database.The function to connect to MySQL is called mysql_connect. This function returns a resource which is a pointer to the database connection. It is also called a database handle.  

$dbhost = "localhost";
    $dbuser = "username";
    $dbpass = "password";
    $dbname = "databasename";
    // Create connection
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
        if( $conn ){
            mysql_select_db("$dbname",$connect);
        }
    else{
        die("Could not connect: " . mysql_error());    
    }
    mysql_close($conn);
?>

MYSQLI, PROCEDURAL WAY TO CONNECT WITH DATABASE

MySQLi is a new improved extension for accessing mysql database. It allows procedural and object oriented interface for accessing mysql database. Here we use the MYSQLI Procedural Way to connect with the Database. Connecting is as simple as just instantiating a new instance of MySQLi, we will be using a username of user with a password of pass connecting to the demo database on the localhost host:
Here is an example:

    $dbhost = "localhost";
    $dbuser = "username";
    $dbpass = "password";
    $dbname = "databasename";
    // Create connection
    $mysqli = mysqli_connect($host,$dbname,$user,$pass);
    if (mysqli_connect_errno($mysqli)){
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else{
        echo "Successfully Connected ";
    }
?>

The first parameter is the database server where the database resides. The second and third parameter are username and password and last one is your database name.
MYSQLI, OBJECT ORIENTED WAY TO CONNECT WITH DATABASE

The OOP way is a little bit different from the procedural way. If your code is all procedural,
you should use the procedural way. If you are coding with OOP or using an OOP framework, it would be
better to use the OOP way, just to be consistent.
The first thing we need to do, like with the procedural way, is to initialize the connection.

    $dbhost = "localhost";
    $dbuser = "username";
    $dbpass = "password";
    $dbname = "databasename";
    // Create connection
    $mysqli = new mysqli($dbhost,$dbname,$dbuser,$dbpass);

    // Check connection
    if ($mysqli->connect_errno){
        echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    }
    else{
        echo "Connected successfully";
    }
?>

$mysqli will be the “connection objectâ€Â. We will be able to call method on this object to execute methods on this object to get the query results or close the connection. The parameters are the same as the procedural form:
The server, the database , username & password.

PDO CONNECTION WITH DATABASE

Connections are established by creating instances of the PDO base class. It does not matter which driver you want to use; you always use the PDO class name. The constructor accepts parameters for specifying the database source (known as the DSN) and optionally for the username and password (if any).

Example 1 Connecting to MySQL



    $dbhost = "localhost";
    $dbuser = "username";
    $dbpass = "password";
    $dbname = "databasename";
    // Create connection
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
?>

If there are any connection errors, a PDOException object will be thrown. You may catch the exception if you want to handle the error condition.

Example 2 Handling connection errors

    try {
        $dbhost = "localhost";
        $dbuser = "username";
        $dbpass = "password";
        $dbname = "databasename";
        $dbh = new PDO("mysql:host=$dbhost;dbname=$dbhost", $dbuser, $dbname);
            foreach($dbh->query("SELECT * from FOO") as $row) {
                print_r($row);
            }
        $dbh = null;
    }


Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted you do this by assigning NULL to the variable that holds the object. If you do not do this explicitly, PHP will automatically close the connection when your script ends.


Example 3 Closing a connection

    $dbhost = "localhost";
    $dbuser = "username";
    $dbpass = "password";
    $dbname = "databasename";
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    // use the connection here

    // and now we are done; close it
    $dbh = null;
?>

Tags

OUR HAPPY CLIENTS

Abbott_Laboratories_logo
Club-Mahindra-Logo
airth
zenesty-logo