Skip to content

Fetching records

Fetching records from the database is done by using the from and the select methods.

Basic usage

php
use Sematico\Baselibs\QueryBuilder\QueryBuilder;

$query = new QueryBuilder();

$select = $query->select()->from( 'users' );

The from method is used to specify the table from which to fetch the records. The select method is used to specify the columns to be selected.

To execute the query and fetch all records, you can use the fetch_all method.

php
$records = $select->fetch_all();

Fetching a single record

To fetch a single record, you can use the fetch_row method. Use this when you expect only one row or want to get the first row of the result.

php
$record = $select->fetch_row();

Fetching a single value

To fetch a single value, you can use the fetch_column method. Useful when you only need values from one specific column.

php
$value = $select->fetch_column( $column_offset = 0 );

Refer to the wpdb documentation for more information.

Fetch style

The $fetch_style parameter (for fetch_row and fetch_all) determines how the results are returned:

  • ARRAY_A: Returns an associative array (default)
  • ARRAY_N: Returns a numeric array
  • OBJECT: Returns an object