Google Code Prettify - 輕量級的語法上色工具

星期二, 9月 08, 2015

啟用 PDO 連結資料庫


PHP 5.3 以上已內建PDO模組

php.ini 或 extensions.ini 中啟用這二行
extension=pdo.so
extension=pdo_mysql.so


define('DB_NAME','test');
define('DB_USER','test');
define('DB_PASSWD','test');
define('DB_HOST','localhost');
define('DB_TYPE','mysql');

try( //有沒有try其實沒關係 多學一個try的用法也不錯
//連接MySQL
$dbh = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWD);
//資料庫使用 UTF8 編碼
$dbh->query('SET NAMES UTF8');
} catch (PDOException $e) {
    echo 'Error!: ' . $e->getMessage() . ' ';
}


//連接Access
//http://www.sitepoint.com/using-an-access-database-with-php/
$dbName = $_SERVER["DOCUMENT_ROOT"] . "\products.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}
$dbh = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");