Jump to content

Trouble getting all results from a mysql query


c_shelswell

Recommended Posts

Hi i have a mysql query which should return a number of results to an array. Php is sending the correct queries but for some reason it's only getting 1 result back each time and it should get loads.

here's my code

[code]
function getSomeMedia($mediaId)
{
$conn = db_connect();
foreach ($mediaId as $key => $value)
{
$query = "select title from media where media_id='$value'";
$result = mysql_query($query);
$result = db_result_to_array($result);
}

if (!$result)
return false;

return $result;
}
/*********************** RESULT TO ARRAY *********************/
function db_result_to_array($result){
$res_array = array();
for ($count=0; $row=@mysql_fetch_array($result); $count++)
$res_array[$count] = $row;

return $res_array;
}[/code]

just can't get why it's only getting one result each time.

Any ideas would be great.
Cheers
Your db_result_to_array() function is set up to only get one record. You need to adjust your function to something like this:
[code]
<?php
function db_result_to_array($result) {
  $res = array();
  if (mysql_num_rows($result) > 0) {
    while ($x = mysql_fetch_array($result)) {
      $res[] = $x;
    }
  }
  return $x;
}
?>
[/code]

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.