php pdo database connection class code example

Example 1: php pdo connection

$host     = "localhost";//Ip of database, in this case my host machine    
$user     = "root";	//Username to use
$pass     = "qwerty";//Password for that user
$dbname   = "DB";//Name of the database

try {
    $connection = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(PDOException $e)
{
  echo $e->getMessage();                         
}

Example 2: pdo db connection

<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
?>

Example 3: php pdo sql server connect

$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");

Tags:

Sql Example