rawQueryValue()

Executes a raw SQL query and returns only the first column of the result. If the query contains LIMIT 1, a single value is returned. Otherwise, you’ll get an array with values from the first column of each row. This is ideal for aggregate functions or scalar value extraction.

Signature:

public function rawQueryValue(string $query, ?array $bindParams = null): mixed

Basic Usage (Single Value)

$sql = 'SELECT COUNT(*) FROM test_users WHERE email LIKE ? LIMIT 1';
$count = $db->rawQueryValue($sql, ['%@example.com%']);

Basic Usage (Multiple Values)

$sql = 'SELECT username FROM test_users WHERE active = 1 ORDER BY id ASC';
$usernames = $db->rawQueryValue($sql);

Return Value

  • mixed — Scalar value (if LIMIT 1)
  • array — Values from the first column (if multiple rows)
  • null — If no result or column was found

Notes

  • The column key is determined automatically from the first row
  • Query must be valid SQL and return at least one column
  • This method does not support full row data — use rawQueryOne() for that

See Also