Skip to content

Commit 737195c

Browse files
committed
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.
1 parent 5a3c1f6 commit 737195c

File tree

6 files changed

+19
-7
lines changed

6 files changed

+19
-7
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ PHP 8.1 UPGRADE NOTES
3535
. The mysqlnd.fetch_copy_data ini setting has been removed. However, this
3636
should not result in user-visible behavior changes.
3737

38+
- PDO:
39+
. PDO::ATTR_STRINGIFY_FETCHES now also stringifies values of type bool to
40+
"0" or "1". Previously booleans were not stringified.
41+
3842
- PDO MySQL:
3943
. Integers and floats in result sets will now be returned using native PHP
4044
types instead of strings when using emulated prepared statements. This

ext/pdo/pdo_stmt.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,13 @@ static inline void fetch_value(pdo_stmt_t *stmt, zval *dest, int colno, int *typ
616616

617617
if (stmt->dbh->stringify) {
618618
switch (Z_TYPE_P(dest)) {
619+
case IS_FALSE:
620+
/* Return "0" rather than "", because this is what database drivers that
621+
* don't have a dedicated boolean type would return. */
622+
zval_ptr_dtor_nogc(dest);
623+
ZVAL_INTERNED_STR(dest, ZSTR_CHAR('0'));
624+
break;
625+
case IS_TRUE:
619626
case IS_LONG:
620627
case IS_DOUBLE:
621628
convert_to_string(dest);

ext/pdo_pgsql/tests/bug62593.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
1313
$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
1414
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1515
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
16+
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
1617
$errors = array();
1718

1819
$value = true;

ext/pdo_pgsql/tests/bug71885.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ foreach ([false, true] as $emulate) {
3737
string(3) "ERR"
3838
array(1) {
3939
[0]=>
40-
bool(true)
40+
string(1) "1"
4141
}
4242
array(1) {
4343
[0]=>
44-
bool(true)
44+
string(1) "1"
4545
}
4646
==OK==

ext/pdo_pgsql/tests/bug71885_2.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ foreach ([false, true] as $emulate) {
4040
--EXPECT--
4141
array(1) {
4242
[0]=>
43-
bool(false)
43+
string(1) "0"
4444
}
4545
array(1) {
4646
[0]=>
47-
bool(true)
47+
string(1) "1"
4848
}
4949
array(1) {
5050
[0]=>
51-
bool(false)
51+
string(1) "0"
5252
}
5353
array(1) {
5454
[0]=>
55-
bool(true)
55+
string(1) "1"
5656
}
5757
==OK==

ext/pdo_pgsql/tests/bug75402.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ object(stdClass)#2 (1) {
105105
["sprogress"]=>
106106
string(3) "100"
107107
["bhidden"]=>
108-
bool(false)
108+
string(1) "0"
109109
["sdatetime"]=>
110110
string(19) "2017.10.16 08:36:45"
111111
}

0 commit comments

Comments
 (0)