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