PEAR DB::getOne question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I wondered what the return value of the getOne method in the PEAR DB class
is when no row is found:

$what = $db->getOne("SELECT field FROM table WHERE 1=2");
if($what=="") echo "emptystring";
if($what==false) echo "false";
if($what==NULL) echo "null";
echo $what;

echoes "emptystringfalsenull". Is it possible that the function returns a
value that is an empty string, false and null at the same time?

If I want to check for the existence of a row, is it okay to check for
either of the three possibilities, or is there a preferred one?

$id = $db->getOne("SELECT id FROM table WHERE condition='fulfilled');

// this one?
if($id=="") do_something();
// or this one?
if($id==false) do_something();
// or that one?
if($id != NULL) do_something();

else do_something_else($id);

Thanks for every hint.


Re: PEAR DB::getOne question

Markus Ernst wrote:
Quoted text here. Click to load it

Either use === in the above if statements, or simply replace them with


If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.

Re: PEAR DB::getOne question

Berislav Lopac wrote:
Quoted text here. Click to load it

Thank you - that makes a difference indeed.


Re: PEAR DB::getOne question

 .oO(Markus Ernst)

Quoted text here. Click to load it

The reason is the automatic typecasting of PHP. All '', FALSE and NULL
evaluate to FALSE, so in your case all three if-statements are

The === operator not only compares the values, but also the types.


Site Timeline