On an old php I try to run this script:

while ($event_data = mysql_fetch_assoc($event_db_answer)){ $puth_query_data_answer = mysql_query( " Запрос возвращает массив с ключом 'puth'. " ); while ($puth_query_data = mysql_fetch_assoc($puth_query_data_answer)){ $event_data['event_name'][] = $puth_query_data['puth']; } } 

A two-dimensional array $event_data['event_name'][] is not created.

And here it works:

 $event_db_answer = mysql_query(" возвращает массив с ключем - 'event_name'"); while ($event_data = mysql_fetch_assoc($event_db_answer)){ if($event_data['event_name'] != ''){ if($same_event != $event_data['event_name']){ $same_event = $event_data['event_name']; } if($same_event == $event_data['event_name']){ $event_arr["$same_event"][] = substr($event_data['puth'], 14); } } } 

Tell me what's wrong?

  • Sorry, of course, but why old? In the new (> = 5.3) instead of the standard api mysql (which is officially outdated and no longer served) there is a PDO. There fetch operations return very convenient arrays. He himself broke for a long time, but he had much more opportunities, and many of my problems were resolved. - i am so lame
  • 2
    $event_data each time with a new value from mysql_fetch_assoc . - u_mulder
  • The fact that MySQL functions mysql_ * is a must die, this is true, but the world has not ended on one PDO, and you can use the functions mysql i _ *, similar to the old MySQL functions,

1 answer 1

As suggested by u_mulder, $ event_data is updated with each iteration of the loop. In the second version of $ event_arr, it does not overlap anything. For the first option, create a separate array in which the result will be written:

 $event_data_names = array(); while ($event_data = mysql_fetch_assoc($event_db_answer)){ $puth_query_data_answer = mysql_query("Запрос возвращает массив с ключом 'puth'."); while ($puth_query_data = mysql_fetch_assoc($puth_query_data_answer)){ $event_data_names[] = $puth_query_data['puth']; } }