我试图连接到我的数据库并关注一本书。我写了第一组代码,它没有工作。所以经过一番谷歌搜索后,我找到了第二个代码块。它工作正常,但我很想知道区别,因为我看不到它。
<?php
try
{
$huh = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'test');
$huh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$huh->exec("SET CHARATER SET utf8");
} catch (PDOException $e) {
$ouput = 'Unable to connect to the database server. ' . $e->getMessage();
include 'output.php';
}
$output = 'Database connection established. ' . $e->getMessage();
include 'ouput.php';
?>
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'test');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("SET CHARACTER SET utf8");
} catch (PDOException $e) {
$output = "Error! " . $e->getMessage();
include 'output.php';
}
$output = 'Success!';
include 'output.php'
?>