petroz Posted October 26, 2010 Share Posted October 26, 2010 Hey Guys, I am hoping you can help me find out why a function of mine is only returning 1 record when I am asking for 10. Here is the function I am using to get the data. When I run the $sql that is printed in phpmyadmin.... I get then records. But when I print the $event_data I am only getting 1 record. Any help would be greatly appreciated function get_events($data) { //print_r($data); $lat = $data['lat']; $long = $data['long']; $offset = $data['offset']; $radius = $data['radius']; $amount = $data['amount']; //the events query $sql = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM events WHERE `status` = '0' HAVING distance < '%s' ORDER BY distance LIMIT $amount OFFSET $offset", mysql_real_escape_string($lat), mysql_real_escape_string($long), mysql_real_escape_string($lat), mysql_real_escape_string($radius)); //debugging print_r($sql); $event_data = array(); $event_data = mysql_fetch_array(mysql_query($sql)); //debugging print_r($event_data); return $event_data; } Link to comment https://forums.phpfreaks.com/topic/216913-returning-array-from-function/ Share on other sites More sharing options...
petroz Posted October 26, 2010 Author Share Posted October 26, 2010 NVM.. I just needed to loop through each row into a array... //the events query $sql = sprintf("SELECT *, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM events WHERE `status` = '0' HAVING distance < '%s' ORDER BY distance LIMIT $amount OFFSET $offset", mysql_real_escape_string($lat), mysql_real_escape_string($long), mysql_real_escape_string($lat), mysql_real_escape_string($radius)); $query = mysql_query($sql); if (mysql_num_rows($query) > 0){ foreach (mysql_fetch_array($query) as $row){ $event_data[] = array( "event" => $row['event'], "event_id" => $row['event_id'], "aid" => $row['aid'], "creator" => $row['creator'], "creatorname" => $row['creatorname'], "email" => $row['email'], "longitude" => $row['longitude'], "latitude" => $row['latitude'], "category" => $row['category'], "rating" => $row['rating'] ); } } Link to comment https://forums.phpfreaks.com/topic/216913-returning-array-from-function/#findComment-1126821 Share on other sites More sharing options...
Psycho Posted October 26, 2010 Share Posted October 26, 2010 You don't need to do all this: foreach (mysql_fetch_array($query) as $row){ $event_data[] = array( "event" => $row['event'], "event_id" => $row['event_id'], "aid" => $row['aid'], "creator" => $row['creator'], "creatorname" => $row['creatorname'], "email" => $row['email'], "longitude" => $row['longitude'], "latitude" => $row['latitude'], "category" => $row['category'], "rating" => $row['rating'] ); } Just do this: foreach (mysql_fetch_array($query) as $row) { $event_data[] = $row; } Link to comment https://forums.phpfreaks.com/topic/216913-returning-array-from-function/#findComment-1126836 Share on other sites More sharing options...
petroz Posted October 26, 2010 Author Share Posted October 26, 2010 Thanks! That did seem a lil inefficient. Link to comment https://forums.phpfreaks.com/topic/216913-returning-array-from-function/#findComment-1126837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.