Reputation: 7684
How is it possible to retrieve the mysql_query from a function? For instance:
class A() {
...
function getAll($l=1) {
...
$result = mysql_query($query);
return $result
}
...
}
$a = new A();
$r = $a -> getAll(2);
while ($row = mysql_fetch_assoc($r)) {
// do something
}
This code above does not work when I return $result from the function. However, when I use the mysql_fetch_assoc function in getAll function, it works perfectly. So basically, my question is how to return a mysql result set from a function?
** EDIT **
I actually get no errors. The while statement used liked above will just not execute. But the query works perfectly when I execute it from within the getAll function.
Upvotes: 0
Views: 698
Reputation: 48897
Absolutely it works.
Do a var_dump($r)
to make sure you're getting a resource. If not, double-check your SQL. There may be an error in it.
Also turn on other error reporting at the top of your script (as well as checking mysql_error
) for better clues as to what's wrong:
ini_set('display_errors', true);
error_reporting(E_ALL);
You've edited much of your code so it's difficult to investigate further. Just make sure that your method is not closing the mysql connection.
Upvotes: 1
Reputation: 25060
Don't return the result resource from the query, but rather return the data extracted from that resource:
$data = array();
while ( $row = mysql_fetch_assoc($result) ) {
$data[] = $row;
}
return $data;
Upvotes: 3