Why can't foreach retrieve only one row when using the mysqli class, while loop fetch multiple rows of data?

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

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.