Jump to content

[SOLVED] Returning mySQL result arrays from a function


marcconlon

Recommended Posts

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

<?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.

Archived

This topic is now archived and is closed to further replies.

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