Skip to content

Commit 99bd716

Browse files
committed
Add a fix for PDO::ATTR_STRINGIFY_FETCHES=false with PHP < 8.1
1 parent d120a8d commit 99bd716

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,23 @@ public function query( string $query, ?int $fetch_mode = PDO::FETCH_COLUMN, ...$
786786
$columns = is_array( $this->last_column_meta ) ? $this->last_column_meta : array();
787787
$rows = is_array( $this->last_result ) ? $this->last_result : array();
788788
$affected_rows = is_int( $this->last_return_value ) ? $this->last_return_value : 0;
789+
790+
/**
791+
* With PHP < 8.1, the "PDO::ATTR_STRINGIFY_FETCHES" value of "false"
792+
* is not working correctly with the PDO SQLite driver. In such case,
793+
* we need to manually convert the row values to the correct types.
794+
*/
795+
if ( PHP_VERSION_ID < 80100 && ! $this->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) ) {
796+
foreach ( $row as $i => $value ) {
797+
$type = $columns[ $i ]['native_type'];
798+
if ( 'integer' === $type ) {
799+
$row[ $i ] = (int) $value;
800+
} elseif ( 'float' === $type ) {
801+
$row[ $i ] = (float) $value;
802+
}
803+
}
804+
}
805+
789806
return new WP_PDO_Synthetic_Statement( $this, $columns, $rows, $affected_rows );
790807
} catch ( Throwable $e ) {
791808
try {

0 commit comments

Comments
 (0)