I would say you should not do that because it will increase the likely-hood that there are uncaught exceptions and programmers are used to receiving false or null as a return value when the query errors or returns 0 rows.Jenk wrote:Why not just have the fetch_all throw and exception instead?astions wrote:I like to return false so I can write code like this:
Code: Select all
if (false === $results = $db->mysql_fetch_all("SELECT foo FROM bar")) { throw new Exception("There are no matching foos"); }
mysql_fetch_all()
Moderator: General Moderators
Re: mysql_fetch_all()
Re: mysql_fetch_all()
That's not a function.. it's a method on an object. To not throw exceptions is going against the grain of the majority, I'd have thought. 
Re: mysql_fetch_all()
Two things:
If you want to prevent foreach() warnings when the variable is not an array, use type hints:
That code will generate no errors if $this_array is currently undefined or empty.
Also, WRT the topic at hand, I've had a function like this in my library for a while now. One thing I changed over time though, was instead of only returning a value, I have an extra argument.. note the args you pass I've already worked out within another part of the object class I have the fetch_all as a method of..
This allows you to have the option to pass an array to the function/method that already has data (say, another sql query) and to add to the resultant rows into one bigger array.
Thus when I go (using my db class)
Hope that helps.
If you want to prevent foreach() warnings when the variable is not an array, use type hints:
Code: Select all
foreach ( (array) $this_array as $key=>$value)
{
// yada yada
}
Also, WRT the topic at hand, I've had a function like this in my library for a while now. One thing I changed over time though, was instead of only returning a value, I have an extra argument.. note the args you pass I've already worked out within another part of the object class I have the fetch_all as a method of..
Code: Select all
// *********
public function fetch_all(array &$rows=NULL)
// *********
{
while($row=mysql_fetch_array($this->result))
{
$rows[] = $row;
}
if(isset($rows))
{
return $rows;
}
return NULL;
}
Thus when I go (using my db class)
Code: Select all
//get our rows into a new array
$new_array = $this->db->fetch_all();
// add more rows to a previous result from this sql query
$this->db->fetch_all($old_array);
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: mysql_fetch_all()
There have been some good suggestions, but none that change the basic functionality of the class.
Moved to Snippets.
Moved to Snippets.
(#10850)
Re: mysql_fetch_all()
That's a cast, not a hint, and also means you'll not see any notification of you not sending an array.Dalar_ca wrote:Two things:
If you want to prevent foreach() warnings when the variable is not an array, use type hints:
That code will generate no errors if $this_array is currently undefined or empty.Code: Select all
foreach ( (array) $this_array as $key=>$value) { // yada yada }
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: mysql_fetch_all()
I absolutely loathe that the MySQL library for PHP does not have this function already. Coincidentally, I came across this issue today also.
I think I'll write a patch for the MySQL library and send it to the PHP dev team - those lazy asses.
I think I'll write a patch for the MySQL library and send it to the PHP dev team - those lazy asses.
Re: mysql_fetch_all()
I think that's because MySQLi is being used in MySQL's place, and MySQLi has this function already: mysqli_result::fetch_all.markusn00b wrote:I absolutely loathe that the MySQL library for PHP does not have this function already.