PHP mysqli fetch_array() Function

PHP mysqli fetch_array() Function

❮ php mysqli reference

example – object oriented style

fetch a result row as a numeric array and as an associative array:

Quý khách đang xem: PHP mysqli fetch_array() Function

<?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);

// numeric array
$row = $result -> fetch_array(mysqli_num);
printf (“%s (%s)\n”, $row[0], $row[1]);

// associative array
$row = $result -> fetch_array(mysqli_assoc);
printf (“%s (%s)\n”, $row[“lastname”], $row[“age”]);

Xem nhiều hơn: ERD là gì? | Thinhnotes.com

// free result set
$result -> không tính phí_result();

$mysqli -> close();
?>


look at example of procedural style at the bottom.


definition and usage

the fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both.

note: fieldnames returned from this function are case-sensitive.


syntax

object oriented style:

$mysqli_result -> fetch_array(resulttype)

procedural style:

mysqli_fetch_array(result,resulttype)

parameter values

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:

  • mysqli_assoc
  • mysqli_num
  • mysqli_both (this is default)

technical details

return value: returns an array of strings that corresponds to the fetched row. null if there are no more rows in result-set
php version: 5+

example – procedural style

fetch a result row as a numeric array and as an associative array:

<?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();

Xem nhiều hơn: Tắc kè – Wikipedia tiếng Việt

$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”]);

//
miễn phí result set

mysqli_free_result($result);

mysqli_close($con);

?>



❮ php mysqli reference

Nguồn gốc: https://danhgiaaz.com
danh mục: Hỏi đáp

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *