Why does the Mysqli class use a foreach to fetch only one row, while the while loop pulls out multiple rows of data?

Source: Internet
Author: User
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."
";


?>


Reply to discussion (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.

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

}

Your $rs->fetch_row () has only one row of data to be read with a while loop.

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 $v 2) {

foreach ($v 2 as $v 3) {

Print_r ($v 3);

}
}

But the error.

foreach ($rs->fetch_row () as $key = + $v 2) {       Print_r ($v 2);//Only the first row of data can be fetched here   }


The thing in your foreach () is in itself a problem, $rs->fetch_row () Just take out one line.

Error tip: warning:invalid argument supplied for foreach () in D:\www\mysqli.php on line 13

The internet says that the efficiency of foreach while high, so with foreach, if the problem is re->fetch_array here, that while should also not read out more rows of data?

Reference 1 Floor Yupengbo's reply:
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.

Yupengbo is right, is it possible to conclude that while the data and column data are traversed, foreach can only traverse the column data????

You can understand $rs->fetch_row () This is equivalent to a pointer to 0 for the first time
So while read $rs->fetch_row () is the data out, read it out to continue the next time $rs->fetch_row () This pointer will move down one bit. The data will not end until it is read.

The $rs->fetch_row () in the foreach ($rs->fetch_row () as $v 2) is only treated as an array to handle the end of reading the array of rows inside. No data is read from $rs->fetch_row () in the loop

This should be read at once.

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

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

}

Well
foreach is the loop inside the array,
Notice your first foreach that loop, Fetch_row only run once,

And while that loop, Fetch_array is running 5 times


No words, you should write
foreach ($rs as $row)
Printf_r ($row);

  • 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.