marcconlon Posted April 20, 2007 Share Posted April 20, 2007 Hi there Relative PHP newbie here - really struggling with this one - so any help appreciated! I've always written mySQL queries within each PHP script I write - I'm now trying to get "intelligent" by re-using mySQL queries by referencing them as functions (and passing the Result row back) So, I have a simple mySQL query along the lines of "SELECT * FROM Transactions WHERE CustomerID = 'x''" (the 'x' is passed to the function as a variable - eg - get_customer_transactions(x). This all works fine, to a point. I now want to "return" the array (mysql_fetch_row or mysql_fetch_asscoc) back to the main script - this is the piece I am not getting right. I've seen (and tried) various suggestions such as return $MyArray and return array($MyArray) but they don't seem to be working for me. Could somebody provide a "code demo"? Thanks Marc Link to comment https://forums.phpfreaks.com/topic/47893-solved-returning-mysql-result-arrays-from-a-function/ Share on other sites More sharing options...
per1os Posted April 20, 2007 Share Posted April 20, 2007 <?php function getTransactions($id) { $query = mysql_query("SELECT * FROM Transactions WHERE CustomerID = '" . $id . "'"); if (mysql_num_rows($query) > 1) { whille ($row = mysql_fetch_array($query)) { $return[] = $row; } }elseif (mysql_num_rows($query) == 1) { $return[0] = $row }else { $return = array(); } return $return; } ?> This accounts for more than one record in the query. Link to comment https://forums.phpfreaks.com/topic/47893-solved-returning-mysql-result-arrays-from-a-function/#findComment-234046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.