Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ae78820
Docs: Clarify return value semantics of `wpdb::get_results()`.
apermo May 18, 2026
cdd144a
Docs: Add `@phpstan-return` and tighten types for `wpdb::get_results()`.
apermo Jun 12, 2026
31989de
Docs: Add `@phpstan-return` for `wpdb::get_row()`.
apermo Jun 12, 2026
72a1952
Docs: Add `@phpstan-return` for `wpdb::get_col()`.
apermo Jun 12, 2026
f89caa9
Docs: Clarify null return semantics of `wpdb::get_var()`.
apermo Jun 12, 2026
522405a
Docs: Narrow `@phpstan-param` for `$output` on `wpdb::get_row()` and …
apermo Jun 13, 2026
ba0a402
Docs: Refine return shapes in `@phpstan-return` for `wpdb::get_result…
apermo Jun 13, 2026
fad4875
Docs: Hoist `wpdb::get_var()` empty-string explanation out of `@return`.
apermo Jun 13, 2026
9655035
Docs: Drop blank line between `@return` and `@phpstan-` tags.
apermo Jun 13, 2026
a21d9f4
Merge branch 'trunk' into trac-65261-wpdb-get-results-phpdoc
westonruter Jun 15, 2026
45ca744
Improve see reference for last_error
westonruter Jun 15, 2026
f3cae5c
Suggest ext-mysqli in composer.json
westonruter Jun 15, 2026
326418d
Docs: Assert row array key types in `wpdb::get_row()`, `get_col()`, a…
westonruter Jun 15, 2026
4d8e28b
Eliminate needless setting of key on array to preserve list semantics
westonruter Jun 15, 2026
8883af6
Docs: Assert the column value type in `wpdb::get_var()`.
westonruter Jun 15, 2026
70cbaf7
Use more specific non-empty-string as in return type for get_col() an…
westonruter Jun 15, 2026
d0ce171
Docs: Assert the result key type in `wpdb::get_results()` for OBJECT_K.
westonruter Jun 15, 2026
f102823
Docs: Allow null in `wpdb::get_results()` OBJECT return type.
westonruter Jun 15, 2026
3639132
Docs: Allow integer keys in `wpdb::get_row()` and `get_results()` ARR…
westonruter Jun 15, 2026
015feb1
Fix `wpdb::get_results()` OBJECT_K null key deprecation for SQL NULL …
westonruter Jun 15, 2026
e811575
Merge inline comment with phpdoc
westonruter Jun 15, 2026
1e55aa9
Merge inline comment with phpdoc in get_var
westonruter Jun 15, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"php": ">=7.4"
},
"suggest": {
"ext-dom": "*"
"ext-dom": "*",
"ext-mysqli": "*"
},
"require-dev": {
"composer/ca-bundle": "1.5.12",
Expand Down
94 changes: 81 additions & 13 deletions src/wp-includes/class-wpdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -3019,12 +3019,16 @@ protected function process_field_lengths( $data, $table ) {
* the value in the column and row specified is returned. If $query is null,
* the value in the specified column and row from the previous SQL result is returned.
*
* Returns null both on failure and when the matched cell value is an empty
* string. To distinguish the two cases, check {@see self::$last_error}.
*
* @since 0.71
*
* @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
* @param int $x Optional. Column of value to return. Indexed from 0. Default 0.
* @param int $y Optional. Row of value to return. Indexed from 0. Default 0.
* @return string|null Database query result (as string), or null on failure.
* @return string|null Database query result (as string), or null on failure or when the value is an empty string.
* @phpstan-return non-empty-string|null
*/
public function get_var( $query = null, $x = 0, $y = 0 ) {
$this->func_call = "\$db->get_var(\"$query\", $x, $y)";
Expand All @@ -3039,6 +3043,14 @@ public function get_var( $query = null, $x = 0, $y = 0 ) {

// Extract var out of cached results based on x,y vals.
if ( ! empty( $this->last_result[ $y ] ) ) {
/**
* Column values.
*
* These are returned from the database as strings, or null for SQL NULL, but get_object_vars() types the
* property values as mixed.
*
* @var list<string|null> $values
*/
$values = array_values( get_object_vars( $this->last_result[ $y ] ) );
}

Expand All @@ -3059,6 +3071,24 @@ public function get_var( $query = null, $x = 0, $y = 0 ) {
* respectively. Default OBJECT.
* @param int $y Optional. Row to return. Indexed from 0. Default 0.
* @return array|object|null Database query result in format specified by $output or null on failure.
* @phpstan-param 'OBJECT'|'ARRAY_A'|'ARRAY_N' $output
* @phpstan-return (
* $query is non-falsy-string
* ? (
* $output is 'OBJECT'
* ? stdClass|null
* : (
* $output is 'ARRAY_A'
* ? array<array-key, mixed>|null
* : (
* $output is 'ARRAY_N'
* ? list<mixed>|null
* : null
* )
* )
* )
* : null
* )
*/
public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
Expand Down Expand Up @@ -3104,6 +3134,7 @@ public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
* @param string|null $query Optional. SQL query. Defaults to previous query.
* @param int $x Optional. Column to return. Indexed from 0. Default 0.
* @return array Database query result. Array indexed from 0 by SQL result row number.
* @phpstan-return list<non-empty-string|null>
*/
public function get_col( $query = null, $x = 0 ) {
if ( $query ) {
Expand All @@ -3118,7 +3149,7 @@ public function get_col( $query = null, $x = 0 ) {
// Extract the column values.
if ( $this->last_result ) {
for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
$new_array[ $i ] = $this->get_var( null, $x, $i );
$new_array[] = $this->get_var( null, $x, $i );
Comment on lines -3121 to +3152

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change ensures that PHPStan sees $new_array as a list. The result is an equivalent 0-indexed array.

}
}
return $new_array;
Expand All @@ -3129,18 +3160,47 @@ public function get_col( $query = null, $x = 0 ) {
*
* Executes a SQL query and returns the entire SQL result.
*
* Returns an empty array when no rows match or when the database
* reports an error for the query. Returns null when $query is empty,
* when $output is not one of the recognized constants, or when the
* query cannot run because the connection is not ready.
*
* @since 0.71
*
* @param string $query SQL query.
* @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.
* With one of the first three, return an array of rows indexed
* from 0 by SQL result row number. Each row is an associative array
* (column => value, ...), a numerically indexed array (0 => value, ...),
* or an object ( ->column = value ), respectively. With OBJECT_K,
* return an associative array of row objects keyed by the value
* of each row's first column's value. Duplicate keys are discarded.
* Default OBJECT.
* @return array|object|null Database query results.
* @param string|null $query SQL query.
* @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.
* With one of the first three, return an array of rows indexed
* from 0 by SQL result row number. Each row is an associative array
* (column => value, ...), a numerically indexed array (0 => value, ...),
* or an object ( ->column = value ), respectively. With OBJECT_K,
* return an associative array of row objects keyed by the value
* of each row's first column's value. Duplicate keys are discarded.
* Default OBJECT.
* @return array|null Database query results. Empty array when no rows match
* or on database error. Null when $query is empty, when
* $output is invalid, or when the connection is not ready.
* @phpstan-param 'OBJECT'|'OBJECT_K'|'ARRAY_A'|'ARRAY_N' $output
* @phpstan-return (
* $query is non-falsy-string
* ? (
* $output is 'OBJECT'
* ? list<stdClass>|null

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this case would be rather as follows to match the other $output types:

Suggested change
* ? list<stdClass>|null
* ? list<stdClass>

However, that would introduce a behavior change which should be discussed. Whenever null is to be returned, probably an empty array should be returned instead.

* : (
* $output is 'OBJECT_K'
* ? array<array-key, stdClass>
* : (
* $output is 'ARRAY_A'
* ? list<array<array-key, mixed>>
* : (
* $output is 'ARRAY_N'
* ? list<list<mixed>>
* : null
* )
* )
* )
* )
* : null
* )
*/
public function get_results( $query = null, $output = OBJECT ) {
$this->func_call = "\$db->get_results(\"$query\", $output)";
Expand All @@ -3167,7 +3227,15 @@ public function get_results( $query = null, $output = OBJECT ) {
if ( $this->last_result ) {
foreach ( $this->last_result as $row ) {
$var_by_ref = get_object_vars( $row );
$key = array_shift( $var_by_ref );
/**
* The first column's value is used as the key.
*
* A SQL NULL value surfaces as null here, so coerce it to an empty string to avoid the deprecated
* use of null as an array offset (PHP 8.5+).
*
* @var array-key $key
*/
$key = array_shift( $var_by_ref ) ?? '';
if ( ! isset( $new_array[ $key ] ) ) {
$new_array[ $key ] = $row;
}
Expand Down
Loading