Jump to content

Adding a value from MySQL query array into a PHP array


ben9292

Recommended Posts

So I'm querying my database to add the results (mapID's) into a PHP array.

The MySQL query I used in the below code would usually return 10 values (only 10 mapID's in the database)

 

while($data = mysql_fetch_array(mysql_query("SELECT mapID FROM maps"))){
$sqlsearchdata[] = $data['mapID'];
}

 

Instead the page takes ages to load then gives this error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 16 bytes)

It says the error begins on the first line of the above code. I'm assuming this is not the right way to add the value from the MySQL array into a normal PHP array.

 

Can anyone help me?

Just tested it locally, and building the whole query in the while() loop is cratering it. This should run fine

 

$query = "SELECT mapID FROM maps";
$result = mysql_query( $query ) or die(mysql_error());
while( $array = mysql_fetch_assoc($result) ){
   $sqlsearchdata[] = $array['mapID'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.