Hướng dẫn while mysqli_fetch_array - trong khi mysqli_fetch_array

Hàm mysqli_fetch_array() sẽ tìm và trả về một dòng kết quả của một truy vấn MySQL nào đó dưới dạng một mảng kết hợp, mảng liên tục hoặc cả hai.

Hướng dẫn while mysqli_fetch_array - trong khi mysqli_fetch_array

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: mysqli_fetch_array( $result, $result_type);mysqli_fetch_array( $result, $result_type);

Trong đó::

  • $result là kết quả của truy vấn, là kết quả trả về của các hàm: mysqli_query(), mysqli_store_result() hoặc mysqli_use_result().
  • $result_type là tham số không bắt buộc, quy định kiểu trả về mang một trong các giá trị sau:
    • MYSQLI_ASSOC.
    • MYSQLI_NUM.
    • MYSQLI_BOTH.

Kết quả trả về

Hàm sẽ trả về mảng kết hợp hoặc liên tục chứa thông tin của hàng kết quả.

Bài viết này được đăng tại [free tuts .net]

Ví dụ

Cách sử dụng hàm mysqli_fetch_array():

Code

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);

// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);

// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);

// Free result set
mysqli_free_result($result);

mysqli_close($con);

Tham khảo: w3schools.com

mysqli_fetch_array

(PHP 5, PHP 7, PHP 8)

mysqli_result::fetch_array -- mysqli_fetch_array — Fetch the next row of a result set as an associative, a numeric array, or both -- mysqli_fetch_arrayFetch the next row of a result set as an associative, a numeric array, or both

Description

Object-oriented style

publicmysqli_result::fetch_array(int

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
0 =
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1): array|null|false mysqli_result::fetch_array(int
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
0 =
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1
): array|null|false

mysqli_fetch_array(mysqli_result $result, int

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
0 =
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1): array|null|false
(mysqli_result $result, int
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
0 =
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1
): array|null|false

In addition to storing the data in the numeric indices of the result array, this function can also store the data in associative indices by using the field names of the result set as keys.

If two or more columns of the result have the same name, the last column will take precedence and overwrite any previous data. To access multiple columns with the same name, the numerically indexed version of the row must be used.

Note: Field names returned by this function are case-sensitive.: Field names returned by this function are case-sensitive.

Note: This function sets NULL fields to the PHP

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
5 value.: This function sets NULL fields to the PHP
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
5
value.

Parameters

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
6

Procedural style only: A mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result().mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result().

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
7

This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
8,
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
9, or
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1.
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
8
,
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
9
, or
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1
.

By using the

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
8 constant this function will behave identically to the mysqli_fetch_assoc(), while
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
9 will behave identically to the mysqli_fetch_row() function. The final option
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1 will create a single array with the attributes of both.
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
8
constant this function will behave identically to the mysqli_fetch_assoc(), while
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
9
will behave identically to the mysqli_fetch_row() function. The final option
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1
will create a single array with the attributes of both.

Return Values

Returns an array representing the fetched row,

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
5 if there are no more rows in the result set, or mysqli_fetch_array()5 on failure.
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
5
if there are no more rows in the result set, or mysqli_fetch_array()5 on failure.

Examples

Example #1 mysqli_result::fetch_array() examplemysqli_result::fetch_array() example

Object-oriented style

mysqli_fetch_array()6

mysqli_fetch_array()7

mysqli_fetch_array()8

publicmysqli_result::fetch_array(int

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
0 =
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1): array|null|false

mysqli_fetch_array()6

mysqli_fetch_array()7

mysqli_fetch_array( $result, $result_type);1

mysqli_fetch_array(mysqli_result $result, int

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
0 =
Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
1): array|null|false

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)

In addition to storing the data in the numeric indices of the result array, this function can also store the data in associative indices by using the field names of the result set as keys.

  • If two or more columns of the result have the same name, the last column will take precedence and overwrite any previous data. To access multiple columns with the same name, the numerically indexed version of the row must be used.
  • Note: Field names returned by this function are case-sensitive.
  • Note: This function sets NULL fields to the PHP
    Kabul (AFG)
    Qandahar (AFG)
    Herat (AFG)
    
    5 value.
  • Parameters
  • Kabul (AFG)
    Qandahar (AFG)
    Herat (AFG)
    
    6
  • Procedural style only: A mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result().

Kabul (AFG)
Qandahar (AFG)
Herat (AFG)
7