Problems with PDO Fetch

Source: Internet
Author: User
PDO data



The younger brother encapsulates a function, there are three graphs, I want to take fetch () to the data to do While loop, but there is a dead loop, $roomName is a value

Solving


Reply to discussion (solution)

Your getlistroom called the Fetchdata and returned his results
and Fetchdata executes SQL queries every time.
Fetchdata always returns the first record of the query to the result, unless there is an error or no result found
So it's not possible to use the return value of Fetchdata as the judgment of the loop termination.

You should use Fetchall to return the result set array in the Fetchdata method
Then use the array method to show the result

Thank you, the problem has been solved. But there is a little doubt that you mean that the Fetch method can only be called directly in the loop, but there is no way to encapsulate the fetch.

You can package the fetch, but not like you!
For example, Fetchdata ($sql) is intended to execute SQL instructions and return results
The result may be one or more. So only fetch can not meet the requirements, to use Fetchall
In actual work, there are only a few query results, such as obtaining records, checking whether the user is registered, etc.
You can then define a Fetchrow method that returns only one row with fetch

I see, thanks!

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