Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
DB::select() Always Return 'true'
  • I'm creating package for a11n (authorization). I'm now checking do user exist... Here's my model method, but problem is that it always return 'true'. It should return boolean and 'true' only when that entry exist in the database. Else - 'false'. I can't see my mistake...
    public static function do_exist( $username, $password ) {
    
     $query =
      DB::select( 'id' )
       ->from( 'users' )
       ->where( 'username', '=', $username )
       ->where( 'password', '=', sha1( $password ) );
    
    
     return (boolean) $query;
    
    }
    
    P.S. I used this for guidlines to DB class.
  • First of all: before converting to boolean you should try to see what you actually get. If you had used var_dump($query) or Debug::dump($query) you'd have seen that what you get is a Database_Query_Builder_Select object.
    This query object doesn't know you want to execute it and until you do it will remain a query object. Add ->execute()->as_array() after the last where() and you should get something returned or an empty result.
  • Good explanation. I think it my laziness - so I didn't use var_dump(). ( Can you explain why do I need as_array()? Edit: Ohh... It's still and object. Get it... ^^ Edit #2: Is there something like an API for Fuel? Like there is for Kohana. It would be great to see all DB's class methods and params... maybe even code of them! Edit #3: Don't read Edit #1. I was wrong there... =D Edit #4: I was wrong in Edit #3. Just kill me! My bad... P.S. Without as_array() it's variable that contains object.
  • If I wanna check if there are results found, I do:
    $user = \DB::select()
       ->where('username', '=', $username_email)
       ->or_where('email', '=', $username_email)
       ->from(\Config::get('simpleauth.table_name'))->execute();
    
    if($user->count() > 0)
            {
                    echo 'user found';
    }
    else
    {
            echo 'no user foudn';
    }
    
  • Thank you, both. Here's my final code.... =]
    public static function valid_combo( $username, $password ) {
    
     $query =
      DB::select( 'id' )
       ->from( 'users' )
       ->where( 'username', '=', $username )
       ->where( 'password', '=', sha1( $password ) )
       ->execute();
    
    
     if( $query->count() !== 0 ) {
    
      return true;
    
     }
    
    }
    
  • And if your $query->count is 0? Your function won't return false.
    Try this, also a little simplified if you prefer it this way:
    public static function valid_combo( $username, $password ) {
    
     $query = DB::select( 'id' )
       ->from( 'users' )
       ->where( 'username', '=', $username )
       ->where( 'password', '=', sha1( $password ) )
       ->execute();
       
       return $query->count() > 0;
    }
    
  • Yeah, already done that... =D Thanks again. ))
  • For reference, DB class is now documented.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion