Why use the Mysqli class when foreach can only take one row, while the while loop pulls out multiple rows of data

Source: Internet
Author: User
Why does the Mysqli class use a foreach to fetch only one row, while the while loop pulls out multiple rows of data?
I am a novice, there is a problem please help, is to use the Mysqli class, the data table test has 5 rows of data, with the Foreach function can only take out the first row, the others are not taken out, while using while can be removed 5 rows, why?


$mysqlii =new mysqli ("localhost", "root", "root", "test");

$sql = "SELECT * from user";

$rs = $mysqlii->query ($sql, Mysqli_store_result);

foreach ($rs->fetch_row () as $key = + $v 2) {

Print_r ($v 2); Only the first row of data can be fetched here

}

while (list ($a, $b, $c, $d) = $rs->fetch_array ()) is able to remove 5 rows or all of the data
//
echo $a. " + ". $b." + ". $c."
";


?>
------Solution--------------------
foreach ($rs->fetch_row () as $key = + $v 2) {

Print_r ($v 2); Only the first row of data can be fetched here

}
The function of this write is to loop the data for all columns of the first row.
The $rs->fetch_row () returns an array.
------Solution--------------------
foreach ($rs->fetch_row () as $key = + $v 2) {

Print_r ($v 2); Only the first row of data can be fetched here

}

foreach ($rs->fetch_array () as $key = + $v 2) {

Print_r ($v 2); Only the first row of data can be fetched here

}
------Solution--------------------
References:
References:
foreach ($rs->fetch_row () as $key = + $v 2) {

Print_r ($v 2); Only the first row of data can be fetched here

}
The function of this write is to loop the data for all columns of the first row.
The $rs->fetch_row () returns an array.


I've also considered a two-dimensional array, with
foreach ($rs->fetch_row () as ...

This is still not right!
foreach ($rs->fetch_row () as $v 2)
When you print $v2, the data for each column in the first row is printed,
is a string, not an array.
In this case, you are going to get an error if you foreach ($v 2 as $v 3)!

while ($ary = $rs->fetch_row ())
{
foreach ($ary as $key = + $v 2) {
Print_r ($v 2);
}
}
This is possible.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.