Sunday, 21 June 2015

how to connect to mysql database using pdo in php ?

Connections are established by creating instances of the PDO base class


<?php
try {
$dsn='mysql:host=localhost;dbname=test';
$username='root';
$password='root';

    $con= new PDO($dsn, $username, $password);
 
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

  NOTE : - 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.

2 comments:

  1. Replies
    1. $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.

      'REMOTE_ADDR'
      The IP address from which the user is viewing the current page.

      Delete