Jump to content

returning array from function


petroz

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.