check pdo connection php code example
Example 1: php pdo connection
$host = "localhost";
$user = "root";
$pass = "qwerty";
$dbname = "DB";
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: php PDO database connection
$hostName = "localhost";
$dbName = "test";
$userName = "test";
$password = "test1";
try {
$pdo = new PDO("mysql:host=$hostName;dbname=$dbName",$userName,$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}