HomeDevCentral

Fetch a scalar value from a result

Description

Fetch a scalar value from a result

Summary:
While queryScalar is useful for fetching scalar values in queries like
SELECT count(*), it cannot be used with prepared statements in PDO.

DatabaseResult::fetchScalar returns the first item of the next row.

The following fluent pattern is now supported:

$sql = "SELECT count(*) FROM foo WHERE id = :id";

return $this->db
    ->prepare($sql)
    ->with("id", $this->id)
    ->query()
    ->fetchScalar();

fetchScalar is implemented at DatabaseResult level, making it available
with all drivers, not just PDO.

Ref T2169

Test Plan:

  • New unit test for current Result tests classes
  • Tested with D3835 on Obsidian codebase

Maniphest Tasks: T2169

Differential Revision: https://devcentral.nasqueron.org/D3838

Details

Provenance
derecksonAuthored on Sat, Nov 1, 11:43
derecksonPushed on Sat, Nov 1, 12:10
Differential Revision
D3838: Fetch a scalar value from a result
Parents
rKERUALD5f60b79b4dfd: Implement PDOEngine::initialize
Branches
Unknown
Tags
Unknown
Tasks
T2169: Support prepared statement for PDO drivers