Why can't foreach retrieve only one row when using the mysqli class, while loop fetch multiple rows of data? I am a newbie. if you have any questions, please use the mysqli class. the data table test contains five rows of data. The foreach function can only retrieve the first row. None of the other rows can be retrieved, while, five rows can be taken out. 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 => $ v2 ){
Print_r ($ v2); // only the first row of data can be retrieved here
}
// While (list ($ a, $ B, $ c, $ d) = $ rs-> fetch_array () can retrieve 5 rows, that is, all data
//
// Echo $ a. "+". $ B. "+". $ c ."
";
?>
Reply to discussion (solution)
Foreach ($ rs-> fetch_row () as $ key => $ v2 ){
Print_r ($ v2); // only the first row of data can be retrieved here
}
In this way, the write function outputs the data of all columns in the first row cyclically.
$ Rs-> fetch_row () returns an array.
Foreach ($ rs-> fetch_row () as $ key => $ v2 ){
Print_r ($ v2); // only the first row of data can be retrieved here
}
Foreach ($ rs-> fetch_array () as $ key => $ v2 ){
Print_r ($ v2); // only the first row of data can be retrieved here
}
Your $ rs-> fetch_row () has only one row of data, which is read in a while loop.
Foreach ($ rs-> fetch_row () as $ key => $ v2 ){
Print_r ($ v2); // only the first row of data can be retrieved here
}
In this way, the write function outputs the data of all columns in the first row cyclically.
$ Rs-> fetch_row () returns an array.
I have also considered a two-dimensional array.
Foreach ($ rs-> fetch_row () as $ v2 ){
Foreach ($ v2 as $ v3 ){
Print_r ($ v3 );
}
}
But an error is reported.
Foreach ($ rs-> fetch_row () as $ key => $ v2) {print_r ($ v2); // only the first row of data can be retrieved here}
Something in your foreach () is faulty. $ rs-> fetch_row () only takes one row.
Error message: Warning: Invalid argument supplied for foreach () in D: \ www \ mysqli. php on line 13
It is said on the Internet that foreach is highly efficient while, so if foreach is used, if the problem lies in re-> fetch_array, then while should not read multiple rows of data?
Reply to reference yupengbo on the first floor:
Foreach ($ rs-> fetch_row () as $ key => $ v2 ){
Print_r ($ v2); // only the first row of data can be retrieved here
}
In this way, the write function outputs the data of all columns in the first row cyclically.
$ Rs-> fetch_row () returns an array.
I have also considered a two-dimensional array.
Foreach ($ rs-> fetch_row () ......
This is still wrong!
Foreach ($ rs-> fetch_row () as $ v2)
When you print $ v2, the data in each column in the first row is printed,
Is a string, not an array.
In this case, if you perform foreach ($ v2 as $ v3), an error is returned!
While ($ ary = $ rs-> fetch_row ())
{
Foreach ($ ary as $ key => $ v2 ){
Print_r ($ v2 );
}
}
This is acceptable.
Yupengbo is right. can I draw a conclusion that while can traverse both row and column data, while foreach can only traverse column data ????
It can be understood that $ rs-> fetch_row () is equivalent to a pointer pointing to 0 for the first time.
Therefore, while reading $ rs-> fetch_row () has data, and the pointer $ rs-> fetch_row () will move one to the next time. It does not end until no data is read.
In foreach ($ rs-> fetch_row () as $ v2), $ rs-> fetch_row () is treated as an array. after reading a row of arrays, the result is final. Data is not read from $ rs-> fetch_row () cyclically.
This should be able to be read at one time
Foreach ($ rs-> fetch_all () as $ key => $ v2 ){
Print_r ($ v2); // only the first row of data can be retrieved here
}
Well,
Foreach is a loop in the array,
Note that the first foreach loop, fetch_row only runs once,
While the loop, fetch_array is run 5 times
Speechless, you should write
Foreach ($ rs as $ row)
Printf_r ($ row );