PHP script exiting during foreach loop without error, long echo line workaround

I am currently stuck with some strange error in PHP-Script that has been hiding me for two days right now. Before you ask, I already did some research on the topic and found several topics, but none of the suggested answers worked :( So I wanted to give a detailed description of what is happening (or unsuccessfully: D) and how it can be worked out. hoping some of you have a great idea :)

Situation: I am setting up a web page generated by PHP from MYSQL-Data. The target server is running PHP 5.3.3, my local XAMPP installation is on PHP 5.3.8. Now I see strange behavior getting data from the database. The error code basically looks like this:

$results = array();
foreach($a as $b):
  $results[] = get_MYSQL_Data($b);


Now every time I try to run the code on my local installation the Script just stops executing somewhere inside the loop after 20-25 seconds of loading. Any content created after the Loop is simply not displayed. All error reporting parameters are set to max and even Apache, PHP- or MYSQL-Errorlog doesn't show anything. The loop itself collects about 40 MYSQL records (not many IMHO). The funny thing is, the same Script running on the target web server works like a charm and loads the entire page in 1-2 seconds.

Approaches As the Script runs on the webserver, I compared the phpinfo () output on XAMPP and the webserver and checked every value that is somewhat related to "buffer", "timeout", "max_size", etc. just to find out that they are equal. I also adjusted the values ​​in the local php.ini to match the webserver configuration. None of this had any effect. As mentioned, I have also tried possible solutions from several other posts related, for example setting max_execution_time, etc.

Workaround Since I thought it might be buffer / memory or some kind of problem, I was jokingly trying to overload the buffer as some people do when trying to display the PHP-Progress-Bar. Changing the cycle to

$results = array();
foreach($a as $b):
  $results[] = get_MYSQL_Data($b);
  echo str_repeat(' ', 550);


where 550 is the smallest working number of characters, echo (try and error: D method), suddenly makes Script work. It basically repeats anything with more than 549 characters at the end of the loop word (as I tried to print the $ results-Array as well). This workaround might be fine for development, but is not a production option, so I really need (and desire :)) to fix this.

Since I couldn't find any other setting or change in either Apache, PHP, or MYSQL (although I think it's not MYSQL-related), which I haven't checked or tried, I really hope some of you will have a flash of inspiration to help me :)

Thank you sincerely for your help and best wishes


-------------------- Update --------------------

Ok, so here we go with the full details :) All PHP constants I found represent MYSQL-Column-Names.

This is the function that contains the foreach loop. The passed array $ pruefungen is a simple one-dimensional array that needs to be expanded and from which the "PRUEFUNG_ID" is taken for the MYSQL condition:

function stgGetSemArray($pruefungen) {
  foreach ($pruefungen as $key => $pruefung):
    /* MYSQL-Parameters are stored in Arrays */
    $selects = array(TAB_PERSON_DETAILS_ID);
    $tables = array(TAB_PERSON, TAB_PRUEFUNG);
    $conditions = array(TAB_PRUEFUNG . '.' . TAB_PRUEFUNG . TAB_DETAILS_ID => $pruefung[TAB_PRUEFUNG . TAB_DETAILS_ID]);
    /* MYSQL-Query is conducted */
    $personIDs = db_multipleColumns($selects, $tables, $conditions);
    /* new Data is stored in $pruefungen */
    $pruefungen[$key][TAB_PERSON . TAB_DETAILS_ID] = $personIDs[0];

    /* MYSQL-Parameters are stored in Arrays */
    $selects = getListViewColumns(TAB_PERSON);
    $tables = array_keys($selects);
    $conditions = array(TAB_PERSON . '.' . TAB_PERSON . TAB_DETAILS_ID => $personIDs[0]);
    /* MYSQL-Query is conducted */
    $personNameData = db_multipleColumns($selects, $tables, $conditions);
    /* new Data is stored in $pruefungen (translateAndFOrmat is just some basic texthandling */
    $pruefungen[$key][TAB_PERSON] = translateAndFormatItem(TAB_PERSON,     $personNameData[0], OUTPUT_FORMAT_HEADDING);

    /* yeah, you know..... */
    echo str_repeat(' ', 550);

  return $pruefungen;


The resulting MYSQL tables, tables for their transfer and conditions for MYSQL are stored in arrays and processed by the db_multipleColumns () function, which looks like this:

function db_multipleColumns($select, $table, $condition = array()) {
  global $con; //the MYSQLi-Connection

  /* Query-Columns */
  $query = 'SELECT ' . implode(', ', $select);

  /* Query-Tables, getDirectRelation returns a String like "pruefung LEFT JOIN dozent ON pruefung.dozentID=dozent.dozentID" */
  $query.= ' FROM '.getDirectRelation($table);

  /* Query-Conditions (only if passed) */
  $conditions = array();
  foreach ($condition as $condColumn => $condValue):
    $conditions[] = $condColumn . ' IN (' . implode(', ', $condValue) . ')';
  if (!doCheckVarEmpty($conditions)):
    $query .= ' WHERE ' . implode(' AND ', $conditions);

  /* submit Query */
  $data = $con->query($query) or die($con->error);

  /* fetch data from Query */
  $resultMatrix = array();
  while ($row = $data->fetch_assoc()):
    $resultMatrix[] = $row;

  /* clear MYSQL-Memory */

  return $resultMatrix;


The MYSQL-Query generated by the functions looks like this:

    SELECT person.person_ID, person.vorname, person.nachname, FROM person LEFT JOIN titel ON person.titel_ID=titel.titel_ID WHERE person.person_ID=1


The records returned from the first five loops are as follows:

    person_ID | vorname     | nachname  | name
    1         | Gerd        | Bacher    | Prof. Dr. rer. nat.
    2         | Franz-Josef | Tegude    | Prof. Dr. rer. nat.
    3         | Roland      | Schmechel | Prof. Dr. rer. nat.
    4         | Frank Einar | Kruis     | Prof. Dr.-Ing.
    5         | Wolfgang    | Mertin    | Dr.-Ing.


As already mentioned, the loop performs about 30-40 iterations.

Hope this helps us in some way :)


source to share

1 answer

if $ a has a valid array of valid data that $ b can run get_MYSQL_Data ($ b) ... then have you checked if get_MYSQL_Data ($ b) is hanging? I don't see the source code for get_MYSQL_Data ($ b) and therefore would have to assume that invalid data was sent to it or it hung for some other reason.

Sample data (source code, not examples) for $ a and function get_MYSQL_Data would be quite helpful. :)



All Articles