Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to get generated query for insert/update.
  • If I generate a MySQL query to select data (see code below), the variable $result->_query will contain the actual MySQL query generated. This is useful for debugging. I haven't been able to get the actual MySQL query when doing inserts or updates (see code below). Is there a way to get the query? Select code:
    $query = DB::select('test')->from('examples');
    $result = $query->as_object()->execute();
    $data = $result->as_array();
    

    Update code:
    $query = DB::update('examples')->set($set_array)->where('example_id', '=', $example_id);
    $rows_affected = $query->execute();
    
  • I believe you need DB::last_query(). Place it immediately following the query. Carl
  • Just echo the query before using ->execute() or cast it to a string, the Database Query object has a __toString() method that will return the query as build up to that point.
  • Thanks. Both answers are useful. It turns out I can enter DB::last_query() into my debugger watch window and view the query.

Howdy, Stranger!

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

In this Discussion