Question about where php saves MySQL/MsSQL result resource?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WilliamLou
Forum Newbie
Posts: 12
Joined: Fri Mar 14, 2008 1:32 pm

Question about where php saves MySQL/MsSQL result resource?

Post by WilliamLou »

Hi there:
I have a script to save large datasets from a mssql query into an array, however it will fails into : "[23-Apr-2009 11:31:30] PHP Fatal error: Allowed memory size of 786432000 bytes exhausted (tried to allocate 40 bytes) in /gui/include/MssqlDB.php on line 61
Actually, the total dataset I want to fetch is about 54M, and the memory limit I set in php.ini is already 750M.
Here is the method I used in my MssqlDB class:

Code: Select all

 
  function query ( $query_str ) 
  {
    global $g_logging;
    $result = mssql_query($query_str);
    
    if ($result === FALSE)
      {
    $g_logging->error("mssql query '$query_str' failed");
    return FALSE;
      }
    else
      {
    $g_logging->debug("Successfully send mssql query '$query_str' ");
    return $result;
      }
  }
function get_result( $query_str )
  {
    global $g_logging;
    
    $result = array();
    $queryID = $this->query($query_str);
    if ($queryID !== FALSE)
      {
    if (mssql_num_rows($queryID) >0)
      {
        while ($row=mssql_fetch_row($queryID))
          {
        $result[]=$row;
          }
      }
    else
      {
        $g_logging->info("No result returned by query '$query_str' ");
      }
      }
    else
      $result=FALSE;
    
    if (mssql_free_result($queryID) === FALSE)
      $g_logging->error("failed to free mssql resource");
    
    return $result;    
  }
 
$db = new MssqlDB;
$queryStr = "xxxxxxxxxx";
$db->get_result($queryStr);
 
When I call the method 'get_result', php will pop out the memory exhausted error. I really don't understand why it is.

Actually, I tried to use some procedural approach to test this:

Code: Select all

 
$result = mssql_query($queryStr);
while ( $row = mssql_fetch_row( $result) )
{
 fwrite($file_pointer, $row);
}
 
In this approach, php won't have any errors.
Now, my question is: if the $result (mssql or mysql or any other database resource ) is saved in memory by php engine, why my first approach fails?

Thank you!
Post Reply