Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Query Builder to Plain SQL for Debbuging Purposes?
  • Hey there! I'm debugging my code right now and I would love to have somehow returned plain/SQL text from Query Builder object like...
    \DB::select( 'id' )
      ->from( 'users' )
      ->where( 'username', '=', $username )
      ->where( 'password', '=', $password )
      ->execute();
    

    ...to:
    SELECT id FROM users WHERE username = '$username' AND password = '$password'
    
  • That's actually surprisingly easy:
    $query = \DB::select( 'id' )
      ->from( 'users' )
      ->where( 'username', '=', $username )
      ->where( 'password', '=', $password )
      ->execute();
    
    $sql = (string) $query;
    

    (echoing will work as well, or any other way to cast the object to a string) EDIT: check below, the above is wrong you can only cast to string when the query is not yet executed.
  • Hmm... not so easy.
    ErrorException [ 4096 ]: Object of class Fuel\Core\Database_MySQL_Result could not be converted to string
    
  • I can't see toString() or something equivalent that would print out $_result in Database_Result class.
  • oops, made a mistake:
    $query = \DB::select( 'id' )
      ->from( 'users' )
      ->where( 'username', '=', $username )
      ->where( 'password', '=', $password );
    
    $sql = (string) $query;
    $result = $query->execute();
    
  • Works as charm, thanks.

Howdy, Stranger!

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

In this Discussion