PHP 連接 MySQL 資料庫

物件 ( MySQLi Object-Oriented ):

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// 建立連線
$conn = new mysqli($servername, $username, $password);

// 檢查連線若錯誤顯示訊息
if ($conn->connect_error) {
    die("連線失敗: " . $conn->connect_error);
}
echo "連線成功";
?>

程序式( MySQLi Procedural ):

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// 建立連線
$conn = mysqli_connect($servername, $username, $password);

// 檢查連線若錯誤顯示訊息
if (!$conn) {
    die("連線失敗: " . mysqli_connect_error()); 
}
echo "連線成功";
?>
一般系統出錯時不建議將錯誤訊息直接顯示,建議透過Mail的方式將錯誤訊息回報給工程師
錯誤Mail回報:http://www.gocar.idv.tw/archives/40