致命错误:内存不足(分配524288)(试图分配4294967293字节)

时间:2016-06-24 21:07:30

标签: php database-connection

设置

服务器:Win7
数据库:MSSQLServer 2012
WebServer:IIS
PHP:5.5
连接:$driver="Driver={SQL Server Native Client 11.0}

SELECT auditid, Datalength(comments)
FROM tblAudit
ORDER BY 2 desc

278 33152 (Equates to 16576 characters when viewing in Notepad++)
239 22308 (Equates to 11186 characters when viewed in Notepad++)

php memory settings
ini_set("memory_limit", "-1");

PHP

$Comments = odbc_result($rsAudit,"Comments");

适用于22308字符串长度

用33152

Fatal error: Out of memory (allocated 524288) (tried to allocate 4294967293 bytes) in D:\Inetpub\wwwroot\TramsWeb\php\auditsummary.php on line 235

16,576似乎不是一次性选择的很多角色,但它正在崩溃。

有人可以建议解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

我将从编号4294967293开始猜测你运行的是32位PHP,并且遇到了所有32位内容都受到的~4GB最大内存限制错误。

答案 1 :(得分:0)

对我而言,它看起来像是一个PHP的错误。为了从数据库中收集长字符串,不需要分配该内存量。

建议转向64位php是一个好主意。但是我不愿意这样做,因为它需要客户端做同样的事情,并且他们还有其他依赖关系。

我设计了一种解决方法,以4k块的形式选择数据,然后将它们拼凑在一起:

function get_comments ($conn,$AuditID,$chunksize,$chunks)
# NRE 24-Jun-2016
# If comments contains over 22308 characters, 32 bit php crashes attempting to collect comments in one go
# Workaround is to split select into chucks then piece them to gether
# Chunksize : size of chunk of data to be grabbed in one go. Testing has indicated can be more more than 4K
# $chunks : Number of sections that comments need to be split into to get all the data
{
  $sSql ="SELECT ";
  $x = 1; 
  $lStart=$x;
  $lEnd=$chunksize;
  # Loop round specified number of chunks, building up sql to collect chunks of the data
  while($x <= $chunks) {
    if ($x>1) {
      # If not first chunk, add comma to split chunk selects
      $sSql = $sSql.", ";
    }
    # SUBSTRING chunk size bit of the data. Alias provided for clarity; is not actively used
    $sSql = $sSql."SUBSTRING(comments, ".$lStart.",".$chunksize.") comments".$x."\r\n";
    $lStart=$lStart+$chunksize;
    $lEnd = $lEnd+$chunksize;
    $x++;
  }
  # Complete the sql
  $sSql = $sSql." FROM tblAudit WHERE AuditID = ".$AuditID;
  $rs = odbc_exec($conn, $sSql);
  while(odbc_fetch_row($rs)){
    $x=1;
    while($x <= $chunks) {
      $Comments = $Comments.odbc_result($rs,$x);    
      $x++;
    }
  }     
  # Return the completed comments
  return $Comments;  
}