When a while loop encounters problems

Source: Internet
Author: User
If you encounter a while loop problem, please advise! The problem is as follows: there is an stu table where all the data should be retrieved and the code is written as follows: the result is as follows: what I cannot understand is that the result of mysql_fetch_assoc ($ rs) must be passed to a variable before a positive solution can be obtained. all results cannot be obtained directly in the while loop. I just learned PHP, let the man laugh at me. the tall man gave me some advice! ---- Questions about the while loop, please advise!
The problem is: there is an stu table that needs to retrieve all the data in it,
The code is as follows:

The result is as follows:

Later, I made the following corrections:

The result is correct:

What I cannot understand is that the result of mysql_fetch_assoc ($ rs) must be passed to a variable before a positive solution can be obtained. all results cannot be obtained directly in the while loop. I just learned PHP, let the man laugh at me. the tall man gave me some advice!
------ Solution --------------------
Mysql_fetch_assoc ($ rs) moves the pointer of the result set ($ rs ).
Therefore, while (mysql_fetch_assoc ($ rs) skips some records, because no one takes over the records.
------ Solution --------------------
You executed mysql_fetch_assoc twice.
------ Solution --------------------
Mysql_fetch_assoc ($ rs );
Reads the current record and moves the record pointer to the next row.

While (mysql_fetch_assoc ($ rs) {// here only read, but no output, and the record pointer is moved to the next record
Print_r (mysql_fetch_assoc ($ rs); // Read and output, record pointer moved to the next record
}

Therefore, records with even numbers of programs executed will be output, while records with odd numbers will be skipped without output.

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.