Jump to content

Can I return values from a mysql query from inside a function?


poleposters

Recommended Posts

Hi,

 

I've just recently discovered how useful writing my own php functions are. I've been using them mostly to tidy up repetitive code.

 

However I want to know if this is possible. I have a mysql query which retrieves a customer's details from the database. I use it quite alot and want to place it inside a function. However I'm not sure how to return the values once the query is completed. I thought that the function might work similarly to an include, but this doesn't seem to be the case.

 

Is a function the right way to go about this?

 

 

$query="SELECT * FROM customers LEFT JOIN orders ON orders.customer_id=customers.customer_id LEFT JOIN contact ON contact.customer_id=customers.customer_id WHERE orders.order_id='$order_id'";
		$result=mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		if(mysql_num_rows($result)>0)
		{
			while($select=mysql_fetch_array($result))
			{
			$first_names=$select['first_name'];
			$email=addslashes($select['email']);
			$invoice_location=$select['invoice_location'];
			}

		}	

 

Link to comment
Share on other sites

In this case, because your needing to return multiple values, you'll need to return an array. eg;

 

$arr = array();
while($select = mysql_fetch_array($result)) {
  $arr[] = $select;
}
return $arr;

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.