2019鐵人賽
var_dump()
die()
使用 mysqli 類別建立資料庫物件的寫法。
<?php
// 建立連線
$db_link = new mysqli('127.0.0.1', 'root', '', '', '3306');
// 連線失敗
if (!$db_link) {
die("Connection failed: " . $db_link->connect_error);
}
// 選擇資料庫
$select_db = $db_link->select_db('school');
//// 選擇資料庫失敗
if (!$select_db) {
die("Select database failed: " . $db_link->error);
}
// Query Database
$result = $db_link->query("SELECT * FROM student");
// 將 query 結果取出
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "id: {$row['id']}, name: {$row['name']}, birthday: {$row['birthday']}, address: {$row['address']}\n";
}
} else {
echo "0 row";
}
// 斷開連線
$db_link->close();
?>
結果
id: 1, name: Hank, birthday: 1911-01-01, address: Taiwan
id: 2, name: Jim, birthday: 1921-12-05, address: China
reference from http://php.net/manual/en/class.mysqli.php
reference from http://php.net/manual/en/mysqli.error.php
reference from http://php.net/manual/en/mysqli.select-db.php
reference from http://php.net/manual/en/mysqli.query.php
reference from http://php.net/manual/en/mysqli-result.num-rows.php
reference from http://php.net/manual/en/mysqli-result.fetch-assoc.php
reference from http://php.net/manual/en/mysqli.close.php