From 737195c3ae6ac53b9501cfc39cc80fd462909c82 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 21 Dec 2020 15:31:15 +0100 Subject: PDO: Honor ATTR_STRINGIFY_FETCHES for booleans Of the important PDO drivers, this affects only PDO PgSQL, as both MySQL and SQLite do not return native boolean types. --- UPGRADING | 4 ++++ ext/pdo/pdo_stmt.c | 7 +++++++ ext/pdo_pgsql/tests/bug62593.phpt | 1 + ext/pdo_pgsql/tests/bug71885.phpt | 4 ++-- ext/pdo_pgsql/tests/bug71885_2.phpt | 8 ++++---- ext/pdo_pgsql/tests/bug75402.phpt | 2 +- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/UPGRADING b/UPGRADING index c659515f51..222b30b3e9 100644 --- a/UPGRADING +++ b/UPGRADING @@ -35,6 +35,10 @@ PHP 8.1 UPGRADE NOTES . The mysqlnd.fetch_copy_data ini setting has been removed. However, this should not result in user-visible behavior changes. +- PDO: + . PDO::ATTR_STRINGIFY_FETCHES now also stringifies values of type bool to + "0" or "1". Previously booleans were not stringified. + - PDO MySQL: . Integers and floats in result sets will now be returned using native PHP types instead of strings when using emulated prepared statements. This diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 7ea0f6df0f..e1c51ef29a 100644 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -616,6 +616,13 @@ static inline void fetch_value(pdo_stmt_t *stmt, zval *dest, int colno, int *typ if (stmt->dbh->stringify) { switch (Z_TYPE_P(dest)) { + case IS_FALSE: + /* Return "0" rather than "", because this is what database drivers that + * don't have a dedicated boolean type would return. */ + zval_ptr_dtor_nogc(dest); + ZVAL_INTERNED_STR(dest, ZSTR_CHAR('0')); + break; + case IS_TRUE: case IS_LONG: case IS_DOUBLE: convert_to_string(dest); diff --git a/ext/pdo_pgsql/tests/bug62593.phpt b/ext/pdo_pgsql/tests/bug62593.phpt index 598307a652..a50bcdbc25 100644 --- a/ext/pdo_pgsql/tests/bug62593.phpt +++ b/ext/pdo_pgsql/tests/bug62593.phpt @@ -13,6 +13,7 @@ require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc'; $db = PDOTest::test_factory(__DIR__ . '/common.phpt'); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); +$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); $errors = array(); $value = true; diff --git a/ext/pdo_pgsql/tests/bug71885.phpt b/ext/pdo_pgsql/tests/bug71885.phpt index f47ffcb444..a0a72b6796 100644 --- a/ext/pdo_pgsql/tests/bug71885.phpt +++ b/ext/pdo_pgsql/tests/bug71885.phpt @@ -37,10 +37,10 @@ foreach ([false, true] as $emulate) { string(3) "ERR" array(1) { [0]=> - bool(true) + string(1) "1" } array(1) { [0]=> - bool(true) + string(1) "1" } ==OK== diff --git a/ext/pdo_pgsql/tests/bug71885_2.phpt b/ext/pdo_pgsql/tests/bug71885_2.phpt index 2f9b9923b0..334899db48 100644 --- a/ext/pdo_pgsql/tests/bug71885_2.phpt +++ b/ext/pdo_pgsql/tests/bug71885_2.phpt @@ -40,18 +40,18 @@ foreach ([false, true] as $emulate) { --EXPECT-- array(1) { [0]=> - bool(false) + string(1) "0" } array(1) { [0]=> - bool(true) + string(1) "1" } array(1) { [0]=> - bool(false) + string(1) "0" } array(1) { [0]=> - bool(true) + string(1) "1" } ==OK== diff --git a/ext/pdo_pgsql/tests/bug75402.phpt b/ext/pdo_pgsql/tests/bug75402.phpt index 3aa26660ef..5db01b16b5 100644 --- a/ext/pdo_pgsql/tests/bug75402.phpt +++ b/ext/pdo_pgsql/tests/bug75402.phpt @@ -105,7 +105,7 @@ object(stdClass)#2 (1) { ["sprogress"]=> string(3) "100" ["bhidden"]=> - bool(false) + string(1) "0" ["sdatetime"]=> string(19) "2017.10.16 08:36:45" } -- cgit v1.2.1