How to select a MAX value from column in Query Builder in Kohana framework?-Collection of common programming errors

I need to INSERT a data to table, but before a query I must to know the MAX value from column position, than to INSERT a data WHERE my SELECTED before position+1. Is it possible with query builder?

following my first comment I did query:

$p = DB::select(array(DB::expr('MAX(`position`)', 'p')))->from('supercategories')->execute();

echo $p;

the error:

ErrorException [ Notice ]: Undefined offset: 1

MODPATH\database\classes\kohana\database.php [ 505 ]

500      */
501     public function quote_column($column)
502     {
503         if (is_array($column))
504         {
505             list($column, $alias) = $column;
506         }
507 
508         if ($column instanceof Database_Query)
509         {
510             // Create a sub-query

upd

$p = DB::select(array(DB::expr('MAX(position)'), 'p'))->from('supercategories')->execute();

I used this. But how to get a result?

Solution

$p = DB::select(array(DB::expr('MAX(position)'), 'p'))->from('supercategories')->execute()->get('p');
  1. The total solution would look like:

    1. DB::select(array(DB::expr('MAX(position)'), 'p'))
    2. To retrieve the value use get('p') or the $result->p

Originally posted 2013-11-27 12:26:35.