How to use fetch in php
This article introduces the relevant knowledge of "how to use fetch in php". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
In PHP, fetch refers to the fetch method for querying data. The commonly used fetch methods in PHP are mysqli_fetch_assoc, mysqli_fetch_row, mysqli_fetch_array, mysqli_fetch_object.
Operating environment: Windows 7, PHP 7.4, DELL G3
What does fetch mean in PHP?
The fetch in php refers to the fetch method for querying data.
The difference between the four fetches commonly used in PHP
In PHP, mysqli_fetch is often used to fetch data from the result set returned from the database, the most common being:
mysqli_fetch_assoc(),mysqli_fetch_row(),mysqli_fetch_array(),mysqli_fetch_object(), when used, differ as follows:
$publicMes = mysqli_fetch_assoc($res);var_dump($publicMes);
The results are as follows:
$a = mysqli_fetch_row($res); var_dump($a);
results are as follows
$b = mysqli_fetch_array($res); var_dump($b); echo $b[1]. ''; echo $b['user'];
The results are as follows:
$c = mysqli_fetch_object($res); var_dump($c);
The results are as follows:
Summary:
fetch_assoc() returns an associative array, using database fields as key values,
fetch_row() returns an indexed array, using index values,
fetch_array() returns an associative array, with both index numbers and fields, which can take values in both ways.
fetch_object() returns an object.
Note:
All fetches fetch data from the result set returned from the database only one at a time, the pointer moves down after fetching, and then fetches a new one next time, so you need to use the while loop to fetch all values. If you use a fetch before the loop, the data will be incomplete.
"How to use fetch in php" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!