What if I call the database using a single-state mode?

Source: Internet
Author: User
If you call the database using a single-state mode, the method has a return result set, and the database connection object
I know that the connection object is destroyed at the time of destruction, ask how to release the returned result set, I know can be assigned to the array in the method and then return the array, release the result set, but sometimes need to return the result set instead of the array, please release the result set should be called?
Do you want to write another method like free () in the class and call it outside of the class? I feel that this is not good to write, how do you deal with AH.


Reply to discussion (solution)

If the result set resource is returned, then there is no need to encapsulate the database operation.
Like what

$rs = Db::getinstance ()->query ($sql), $row = Mysql_fetch_assoc ($RS);
Don't you think it's weird?

The purpose of encapsulation is to simplify operations and naturally lose some flexibility
The singleton database object is obviously only expected to cache the result set of the last query, or you will not be confused whether you want to read or release it because you do not know which result set to manipulate.

If the result set resource is returned, then there is no need to encapsulate the database operation.
Like what

$rs = Db::getinstance ()->query ($sql), $row = Mysql_fetch_assoc ($RS);
Don't you think it's weird?

The purpose of encapsulation is to simplify operations and naturally lose some flexibility
The singleton database object is obviously only expected to cache the result set of the last query, or you will not be confused whether you want to read or release it because you do not know which result set to manipulate.

Thank you moderator Swift reply?
Returns the result set is not a matter of encapsulation, just looked at the next online many database operation classes are returned result sets Ah, dizzy AH
Moderator can be bothered to teach under, in addition to ask under the single-State connection database project use more?

Identify the difference between the result set resource and the result set object first
A resource number returned by the result set resource (resource) such as mysql_query (' Select ... ')
A Mysqli_result object returned by the result set object (object) such as Mysqli_query (' Select ... ')

You encapsulate the database class yourself, and you can let Db::query return an object (such as Db_result)

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