xxxxxxxxxx
$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");
xxxxxxxxxx
$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();
}
xxxxxxxxxx
<?php
$db_src = "mysql:host=localhost;dbname=databasename";
$db_user = "root";
$db_pass = "";
try{
$connect = new PDO($db_src, $db_user, $db_pass);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOEXCEPTION $err){
echo "Failed To Connect. Error " . $err->getMessage();
}
/* Variable $connect will be used to execute DB Statements*/
xxxxxxxxxx
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
xxxxxxxxxx
$connect = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
xxxxxxxxxx
<?php
require 'config.php';
$dsn = "mysql:host=$host;dbname=$db;charset=UTF8";
try {
$pdo = new PDO($dsn, $user, $password);
if ($pdo) {
echo "Connected to the $db database successfully!";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
Code language: HTML, XML (xml)