records_query_one()

Like records_query() but returns a single record — the first match. Use it when you expect exactly one record to exist.

Syntax

records_query_one(table, filters?, options?)

Parameters

Name Type Required Description
table string yes Table name or database table id.
filters array no Filter conditions — see records_query().
options array no order, offset. limit is forced to 1.

Returns

A single record array. Throws if no record matches (so you don't silently work with a missing record).

Example

$settings = records_query_one("config", [
    ["field" => "key", "op" => "=", "value" => "tax_rate"]
]);
sys_log("Tax rate: " . $settings["value"]);

Example output

["key" => "tax_rate", "value" => "0.08", "_meta" => ["id" => 3, "title" => "tax_rate"]]

Notes

  • Wrap in try … catch if a missing record is a valid outcome you want to handle:
try {
    $row = records_query_one("config", ["field" => "key", "op" => "=", "value" => "x"](/help/field-key-op-value-x));
} catch ( Exception $e ) {
    $row = null;
}

See also: records_query(), record_get()