how does mysql_fetch_*() work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    how does mysql_fetch_*() work?

    Hi guys,

    reading a recent thread, I wonder if the mysql_fetch_*() functions make a DB connection every time they are called. has anybody some in-depth knowledge or can point me to a resource (google lists all the various manual entries for a long time, if I search)?

    Thanks
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    It's my understanding that the _fetch_ commands just return an array of the most recently requested values from the active or requested connection and moves the file pointer on the underlying database.

    The best way to know for sure is to look inside of the driver implementation at the "C/C++" level, which I'm putting on the burner for my own curiosity.

    I'd think it would be very, very, inefficient and expensive to make connections every time a fetch is invoked, so I'd have to say no.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      I seem to recall reading - although I can't remember the source - that PHP buffers the entire result set of the mysql_query call in memory, from which the mysql_fetch_*, and other mysql functions, access it.

      The description of the mysql_unbuffere d_query function supports that:
      Originally posted by php.net/mysql_unbuffere d_query
      mysql_unbuffere d_query() sends the SQL query query to MySQL without automatically fetching and buffering the result rows as mysql_query() does. This saves a considerable amount of memory with SQL queries that produce large result sets, and you can start working on the result set immediately ...

      Comment

      • dgreenhouse
        Recognized Expert Contributor
        • May 2008
        • 250

        #4
        This is a really good thread...

        Now I'm really confused... :-)

        I've been Googling a bit and it seems as though the memory constraints are on MySQL and not necessarily PHP. As you may notice, you can select some very large result sets with PHP and most PHP configurations are setup with fairly low memory. So... my thinking is that the memory under question is the memory that MySQL consumes not PHP. I know I can cause a timeout in PHP by requesting a large result set and I assume that's because PHP is waiting on MySQL to complete.

        Googles I've tried so far:
        1- mysql out of memory error
        2- mysql_query out of memory error
        3- Others...

        Also, this seems to put some light on the subject:


        Thanks for the great brain-teaser!

        Be nice if someone who's delved-deep pipes in...

        I'll look into the driver source code and see if I can get a definitive answer.

        Comment

        Working...