Skip to content

Query Results

Darko Gjorgjijoski edited this page Jun 15, 2025 · 2 revisions

The QueryBuilder class provides multiple methods to retrieve results.

These methods can be used, chained, in conjunction with the build ones.

Content

Get Results

get( $output = OBJECT, $callable_mapping = null, $calc_rows = false )

Parameter Type Description
$output int Type of output to return. See $wpdb documentation. Default: OBJECT
$callable_mapping callable Callable used to map results to custom classes. Default: null
$calc_rows bool Flag used to indicate if the query should include SQL_CALC_FOUND_ROWS for easy row count calculations. Default: false

Returns: array collection.

Usage sample:

$results = $builder::create()
    ->select( '*' )
    ->from( 'table_a' )
    ->get();

NOTE: The results in the sample above will be returned as a collection of objects.

Output and mapping

Use $output and $callable_mapping parameters to change the way results are returned, see an example:

$results = $builder::create()
    ->select( '*' )
    ->from( 'table_a' )
    ->get( ARRAY_A, function( $row ) {
        return new MyCustomModel( $row );
    } );

The example above will return the results as a collection of arrays, where each collection will have as keys the column names of the result attached with values. Additionally, the mapping callback is used to map each row returned to a custom data model class called MyCustomModel.

First Row

first( $output = OBJECT )

Parameter Type Description
$output int Type of output to return. See $wpdb documentation. Default: OBJECT

Returns: mixed The first record found in the query results.

Usage sample:

$row= $builder::create()
    ->select( '*' )
    ->from( 'table_a' )
    ->first();

Return A Single Value

value( $x = 0, $y = 0 )

Parameter Type Description
$x int Column number/index of the value to return. Default: 0
$y int Row number/index of the value to return. Default: 0

Returns: mixed The single value requested.

Usage samples:

$first_id = $builder::create()
    ->select( 'a.id, a.name' )
    ->from( 'table_a' )
    ->value();

$first_name = $builder::create()
    ->select( 'a.id, a.name' )
    ->from( 'table_a' )
    ->value( 1 );

Get column values

col( $x = 0, $callable_mapping = null )

Parameter Type Description
$x int Column index. Default: 0
$calc_rows bool Flag used to indicate if the query should include SQL_CALC_FOUND_ROWS for easy row count calculations. Default: false

Returns: array collection.

Usage sample:

$results = $builder::create()
    ->select( 'id' )
    ->select( 'name' )
    ->from( 'table_a' )
    ->col();

NOTE: The results in the sample above will return an array with all the values from the column with index 0 (column id in the query).

Count

count( $column = 1, $bypass_limit = true )

Parameter Type Description
$column int Column index. Default: 1
$bypass_limit bool Flag that indicates if limit and offset should not be considered in count. Default: true

Returns: int Counted records value.

Usage sample:

$count = $builder::create()
    ->select( '*' )
    ->from( 'table_a' )
    ->count();

Rows found

rows_found()

Returns: int Number of rows found in the previous query (get() and col()), only if SQL_CALC_FOUND_ROWS is used and is supported.

Usage sample:

$builder= $builder::create()
    ->select( '*' )
    ->from( 'table_a' );
$results = $builder->get( OBJECT, null, true );
$count = $builder->rows_found();

Execute with no results

query( $sql = '' )

Parameter Type Description
$sql string Sql to execute, if empty, builder will build statements set and execute them. Default: ``

Returns: bool indicating if the statement was executed.

Usage sample:

$has_execured = $builder::create()->query( 'SET sql_prop = 1;' );

raw( $sql )

Parameter Type Description
$sql string Sql to execute.

Returns: bool indicating if the statement was executed.

Usage sample:

$has_executed = $builder::create()->raw( 'SET sql_prop = 1;' );

Update

update()

Returns: bool indicating if the update statement was executed.

Usage sample:

$has_executed = $builder::create()
    ->from( 'posts' )
    ->set( ['post_title' => $post_title] )
    ->update();

Usage with "join" and "where" sample:

$has_executed = $builder::create()
    ->from( 'a as table_a' )
    ->join( 'b as table_b', [
        [
            'key_a' => 'table_a.b_id',
            'key_b' => 'table_b.id',
        ]
    ], true )
    ->set( [
        'raw' => 'table_a.child_name = table_b.name',
        'table_a.counter' => [
            'raw' => 'table_a.counter + 1',
        ],
    ] )
    ->where( ['table_a.status' => 'active'] )
    ->update();

Delete

delete()

Returns: bool indicating if the delete statement was executed.

Usage sample:

$has_executed = $builder::create()
    ->from( 'posts' )
    ->where( ['ID' => $post_id] )
    ->delete();

Usage with "join" sample:

$has_executed = $builder::create()
    ->from( 'a as table_a' )
    ->join( 'b as table_b', [
        [
            'key_a' => 'table_a.b_id',
            'key_b' => 'table_b.id',
        ]
    ], true )
    ->where( ['table_b.id' => null] )
    ->delete();

Clone this wiki locally