PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Mar 29, 2017 6:18 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: mysql_fetch_all()
PostPosted: Fri Jul 31, 2009 8:41 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6883
Jenk wrote:
astions wrote:
I like to return false so I can write code like this:

Syntax: [ Download ] [ Hide ]
 
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.

_________________
Image


Top
 Profile  
 
 Post subject: Re: mysql_fetch_all()
PostPosted: Tue Aug 04, 2009 10:52 am 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London
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. :)


Top
 Profile  
 
 Post subject: Re: mysql_fetch_all()
PostPosted: Sun Aug 16, 2009 1:24 am 
Offline
Forum Newbie

Joined: Sun Aug 16, 2009 12:04 am
Posts: 2
Two things:

If you want to prevent foreach() warnings when the variable is not an array, use type hints:

Syntax: [ Download ] [ Hide ]
 
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..
Syntax: [ Download ] [ Hide ]
 
    //              *********
    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)
Syntax: [ Download ] [ Hide ]
 
//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.


Top
 Profile  
 
 Post subject: Re: mysql_fetch_all()
PostPosted: Sun Aug 16, 2009 2:42 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13385
Location: New York, NY, US
There have been some good suggestions, but none that change the basic functionality of the class.

Moved to Snippets.

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: mysql_fetch_all()
PostPosted: Wed Aug 19, 2009 6:31 am 
Offline
DevNet Master
User avatar

Joined: Mon Sep 19, 2005 6:24 am
Posts: 3587
Location: London
Dalar_ca wrote:
Two things:

If you want to prevent foreach() warnings when the variable is not an array, use type hints:

Syntax: [ Download ] [ Hide ]
 
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. :)


Top
 Profile  
 
 Post subject: Re: mysql_fetch_all()
PostPosted: Sun Oct 11, 2009 3:28 pm 
Offline
Forum Contributor
User avatar

Joined: Sat Oct 20, 2007 2:16 pm
Posts: 298
Location: York, England
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. ;)


Top
 Profile  
 
 Post subject: Re: mysql_fetch_all()
PostPosted: Fri Dec 18, 2009 8:23 am 
Offline
Forum Contributor

Joined: Sat Jan 03, 2009 4:27 pm
Posts: 148
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group