Fungsi PHP mysqli fetch_all()
Contoh - Gaya Berorientasi Objek
Ambil semua baris dan kembalikan hasil-set sebagai array asosiatif:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " .
$mysqli -> connect_error;
exit();
}
$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname";
$result
=
$mysqli -> query($sql);
// Fetch all
$result -> fetch_all(MYSQLI_ASSOC);
// Free result set
$result
-> free_result();
$mysqli -> close();
?>
Lihatlah contoh gaya prosedural di bagian bawah.
Definisi dan Penggunaan
Fungsi fetch_all() / mysqli_fetch_all() mengambil semua baris hasil dan mengembalikan kumpulan hasil sebagai larik asosiatif, larik numerik, atau keduanya.
Catatan: Fungsi ini hanya tersedia dengan MySQL Native Driver.
Sintaksis
Gaya berorientasi objek:
$mysqli_result ->
fetch_all(resulttype)
Gaya prosedural:
mysqli_fetch_all(result, resulttype)
Nilai Parameter
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
resulttype | Optional. Specifies what type of array that should be produced. Can be one of the following values:
|
Detail Teknis
Nilai Kembali: | Mengembalikan array array asosiatif atau numerik yang menyimpan baris hasil |
---|---|
Versi PHP: | 5.3+ |
Contoh - Gaya prosedural
Ambil semua baris dan kembalikan hasil-set sebagai array asosiatif:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname";
$result = mysqli_query($con, $sql);
// Fetch all
mysqli_fetch_all($result, MYSQLI_ASSOC);
// Free result set
mysqli_free_result($result);
mysqli_close($con);
?>
Referensi PHP MySQLi