poleposters Posted June 7, 2010 Share Posted June 7, 2010 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']; } } Quote Link to comment https://forums.phpfreaks.com/topic/204067-can-i-return-values-from-a-mysql-query-from-inside-a-function/ Share on other sites More sharing options...
trq Posted June 7, 2010 Share Posted June 7, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/204067-can-i-return-values-from-a-mysql-query-from-inside-a-function/#findComment-1068836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.