PEAR DB::getOne question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Ernst

    PEAR DB::getOne question

    Hi

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

    $what = $db->getOne("SELE CT field FROM table WHERE 1=2");
    if($what=="") echo "emptystrin g";
    if($what==false ) echo "false";
    if($what==NULL) echo "null";
    echo $what;

    echoes "emptystringfal senull". 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("SELE CT id FROM table WHERE condition='fulf illed');

    // 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_el se($id);


    Thanks for every hint.

    --
    Markus


  • Berislav Lopac

    #2
    Re: PEAR DB::getOne question

    Markus Ernst wrote:[color=blue]
    > Hi
    >
    > I wondered what the return value of the getOne method in the PEAR DB
    > class is when no row is found:
    >
    > $what = $db->getOne("SELE CT field FROM table WHERE 1=2");
    > if($what=="") echo "emptystrin g";
    > if($what==false ) echo "false";
    > if($what==NULL) echo "null";
    > echo $what;[/color]

    Either use === in the above if statements, or simply replace them with
    var_dump($what) .

    Berislav

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


    Comment

    • Markus Ernst

      #3
      Re: PEAR DB::getOne question

      Berislav Lopac wrote:[color=blue]
      > Markus Ernst wrote:[color=green]
      >> Hi
      >>
      >> I wondered what the return value of the getOne method in the PEAR DB
      >> class is when no row is found:
      >>
      >> $what = $db->getOne("SELE CT field FROM table WHERE 1=2");
      >> if($what=="") echo "emptystrin g";
      >> if($what==false ) echo "false";
      >> if($what==NULL) echo "null";
      >> echo $what;[/color]
      >
      > Either use === in the above if statements, or simply replace them with
      > var_dump($what) .
      >
      > Berislav[/color]

      Thank you - that makes a difference indeed.

      --
      Markus


      Comment

      • Michael Fesser

        #4
        Re: PEAR DB::getOne question

        .oO(Markus Ernst)
        [color=blue]
        >Berislav Lopac wrote:
        >[color=green]
        >> Either use === in the above if statements, or simply replace them with
        >> var_dump($what) .[/color]
        >
        >Thank you - that makes a difference indeed.[/color]

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

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

        Micha

        Comment

        Working...