mysql_fetch_all()

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: mysql_fetch_all()

Post by Benjamin »

Jenk wrote:
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");
}
 
Why not just have the fetch_all throw and exception instead?
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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: mysql_fetch_all()

Post by Jenk »

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. :)
Dalar_ca
Forum Newbie
Posts: 2
Joined: Sun Aug 16, 2009 12:04 am

Re: mysql_fetch_all()

Post by Dalar_ca »

Two things:

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

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;
 
    }
 
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)

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);
 
Hope that helps.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: mysql_fetch_all()

Post by Christopher »

There have been some good suggestions, but none that change the basic functionality of the class.

Moved to Snippets.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: mysql_fetch_all()

Post by Jenk »

Dalar_ca wrote:Two things:

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
}
 
That code will generate no errors if $this_array is currently undefined or empty.
That's a cast, not a hint, and also means you'll not see any notification of you not sending an array. :)
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: mysql_fetch_all()

Post by markusn00b »

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. ;)
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: mysql_fetch_all()

Post by MichaelR »

markusn00b wrote:I absolutely loathe that the MySQL library for PHP does not have this function already.
I think that's because MySQLi is being used in MySQL's place, and MySQLi has this function already: mysqli_result::fetch_all.
Post Reply