<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
// 檢查連線
if (mysqli_connect_errno()) {
printf("連線失敗: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
// 執行多個查詢
if ($mysqli->multi_query($query)) {
do {
// 儲存第一個結果集
if ($result = $mysqli->use_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->close();
}
// 顯示分隔線
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
// 關閉連線
$mysqli->close();
?>
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 檢查連線 */
if (mysqli_connect_errno()) {
printf("連線失敗: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* 執行多重查詢 */
if (mysqli_multi_query($link, $query)) {
do {
/* 儲存第一個結果集 */
if ($result = mysqli_use_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
/* 顯示分隔線 */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}
} while (mysqli_next_result($link));
}
/* 關閉連線 */
mysqli_close($link);
?>
my_user@localhost
-----------------
Amersfoort
Maastricht
Dordrecht
Leiden
Haarlemmermeer