Drupal7致命错误:允许的内存大小为134217728字节耗尽(试图分配24个字节)

时间:2013-11-15 16:42:14

标签: drupal-7

我有这个代码连接外部mysql数据库并重新调整列值

   for ($i=0; $i < sizeof($Columns); $i++) {
        //$Columns contains columns names in a current datatable $dt  

        $result3 = Database::getConnection()->query("SELECT ".$Columns[$i]." From ".$dt." ");  
        $field_values = array();
        $index = 0;

                while ($row = $result3->fetchAssoc()) {
                             $field_values [$index] = $row[$Columns[$i]];
                             //drupal_set_message($field_values[$index]);
                             $index++;
         }

代码适用于任何数据表,除了包含thousend列和evry列的数据表包含许多值。在此数据表的情况下,drupal显示与允许的内存相关的致命错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in C:\xampp\htdocs\.....

我想通过尝试只获得几列来逃避这个错误,所以我做了下一个:

while ($row = $result3->fetchAssoc() && $index<=3) {
             }

但drupal仍然显示这个错误,知道如何避免这样的错误?

1 个答案:

答案 0 :(得分:0)

为此使用Batch API可能是个好主意。但是你必须找到可以处理的最小块而不会使服务器过载。由于这高度依赖于您拥有的数据,以及您“不”拥有的数据(未来数据)。您可能希望将其拆分为非常小的块。

也许你可以,

<?php
for ($i=0; $i < sizeof($Columns); $i++) {
  // Send to batch processing.
}

如果这不起作用,那么你必须

<?php
for ($i=0; $i < sizeof($Columns); $i++) {
  // Do a count query get max
  // split into smaller batches using paging in SQL ( hint: LIMIT 0, 10).
  foreach($pages as $page) {
    // Send to batch processing.
  }
}

请参阅我链接到的批处理API文档,它解释了如何定义批处理并将参数传递给它们。