c_shelswell Posted December 29, 2006 Share Posted December 29, 2006 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 Link to comment https://forums.phpfreaks.com/topic/32170-trouble-getting-all-results-from-a-mysql-query/ Share on other sites More sharing options...
obsidian Posted December 29, 2006 Share Posted December 29, 2006 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]<?phpfunction db_result_to_array($result) { $res = array(); if (mysql_num_rows($result) > 0) { while ($x = mysql_fetch_array($result)) { $res[] = $x; } } return $x;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/32170-trouble-getting-all-results-from-a-mysql-query/#findComment-149314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.