Skip to content

FIX GH-11587 PHP8.1: Fixed the condition for result set values to be of native type, making it compatible with previous versions. #11622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 3 additions & 0 deletions ext/pdo/pdo_dbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@ static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /
return false;
}
dbh->stringify = bval;
if (dbh->methods->set_attribute) {
dbh->methods->set_attribute(dbh, attr, value);
}
return true;

case PDO_ATTR_STATEMENT_CLASS: {
Expand Down
24 changes: 18 additions & 6 deletions ext/pdo_mysql/mysql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ static bool pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
switch (attr) {
case PDO_ATTR_AUTOCOMMIT:
if (!pdo_get_bool_param(&bval, val)) {
return false;
PDO_DBG_RETURN(false);
}
/* ignore if the new value equals the old one */
if (dbh->auto_commit ^ bval) {
Expand All @@ -437,7 +437,7 @@ static bool pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)

case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
if (!pdo_get_bool_param(&bval, val)) {
return false;
PDO_DBG_RETURN(false);
}
/* ignore if the new value equals the old one */
((pdo_mysql_db_handle *)dbh->driver_data)->buffered = bval;
Expand All @@ -446,20 +446,32 @@ static bool pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
case PDO_MYSQL_ATTR_DIRECT_QUERY:
case PDO_ATTR_EMULATE_PREPARES:
if (!pdo_get_bool_param(&bval, val)) {
return false;
PDO_DBG_RETURN(false);
}
/* ignore if the new value equals the old one */
((pdo_mysql_db_handle *)dbh->driver_data)->emulate_prepare = bval;
PDO_DBG_RETURN(true);

case PDO_ATTR_FETCH_TABLE_NAMES:
if (!pdo_get_bool_param(&bval, val)) {
return false;
PDO_DBG_RETURN(false);
}
((pdo_mysql_db_handle *)dbh->driver_data)->fetch_table_names = bval;
PDO_DBG_RETURN(true);

#ifndef PDO_USE_MYSQLND
#ifdef PDO_USE_MYSQLND
case PDO_ATTR_STRINGIFY_FETCHES:
if (!pdo_get_bool_param(&bval, val)) {
PDO_DBG_RETURN(false);
}
unsigned int int_and_float_native = !bval;
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
if (mysql_options(H->server, MYSQLND_OPT_INT_AND_FLOAT_NATIVE, (const char *) &int_and_float_native)) {
pdo_mysql_error(dbh);
PDO_DBG_RETURN(false);
}
PDO_DBG_RETURN(true);
#else
case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
if (!pdo_get_long_param(&lval, val)) {
PDO_DBG_RETURN(false);
Expand Down Expand Up @@ -891,7 +903,7 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
}

#ifdef PDO_USE_MYSQLND
unsigned int int_and_float_native = 1;
unsigned int int_and_float_native = !pdo_attr_lval(driver_options, PDO_ATTR_STRINGIFY_FETCHES, dbh->stringify);
if (mysql_options(H->server, MYSQLND_OPT_INT_AND_FLOAT_NATIVE, (const char *) &int_and_float_native)) {
pdo_mysql_error(dbh);
goto cleanup;
Expand Down
163 changes: 163 additions & 0 deletions ext/pdo_mysql/tests/gh-11587.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
--TEST--
GH-11587 PHP8.1: Fixed the condition for result set values to be of native type, making it compatible with previous versions. #11622
--EXTENSIONS--
pdo_mysql
--SKIPIF--
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
if (!extension_loaded('mysqlnd')) die('skip: This test requires the loading of mysqlnd');
?>
--FILE--
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$db = MySQLPDOTest::factory();

$db->exec('DROP TABLE IF EXISTS test');

$createTestTable = <<<SQL
CREATE TABLE test(
id INT,
`float_col` FLOAT(3,2) DEFAULT NULL,
`double_col` DOUBLE(3,2) DEFAULT NULL,
`decimal_col` DECIMAL(3,2) DEFAULT NULL
)
SQL;

$db->exec($createTestTable);

$insertTestTable = <<<SQL
INSERT INTO test(id, float_col, double_col, decimal_col) VALUES(1, 2.60, 3.60, 4.60)
SQL;

$db->exec($insertTestTable);

echo "PDO::ATTR_EMULATE_PREPARES = true, PDO::ATTR_STRINGIFY_FETCHES = true\n";
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$results = $db->query('SELECT * FROM test');
foreach ($results as $result) {
var_dump($result);
}

echo "\n";

echo "PDO::ATTR_EMULATE_PREPARES = true, PDO::ATTR_STRINGIFY_FETCHES = false\n";
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$results = $db->query('SELECT * FROM test');
foreach ($results as $result) {
var_dump($result);
}

echo "\n";

echo "PDO::ATTR_EMULATE_PREPARES = false, PDO::ATTR_STRINGIFY_FETCHES = true\n";
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
$results = $db->query('SELECT * FROM test');
foreach ($results as $result) {
var_dump($result);
}

echo "\n";

echo "PDO::ATTR_EMULATE_PREPARES = false, PDO::ATTR_STRINGIFY_FETCHES = false\n";
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$results = $db->query('SELECT * FROM test');
foreach ($results as $result) {
var_dump($result);
}

echo "\n";

echo 'done!';
?>
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
?>
--EXPECT--
PDO::ATTR_EMULATE_PREPARES = true, PDO::ATTR_STRINGIFY_FETCHES = true
array(8) {
["id"]=>
string(1) "1"
[0]=>
string(1) "1"
["float_col"]=>
string(4) "2.60"
[1]=>
string(4) "2.60"
["double_col"]=>
string(4) "3.60"
[2]=>
string(4) "3.60"
["decimal_col"]=>
string(4) "4.60"
[3]=>
string(4) "4.60"
}

PDO::ATTR_EMULATE_PREPARES = true, PDO::ATTR_STRINGIFY_FETCHES = false
array(8) {
["id"]=>
int(1)
[0]=>
int(1)
["float_col"]=>
float(2.6)
[1]=>
float(2.6)
["double_col"]=>
float(3.6)
[2]=>
float(3.6)
["decimal_col"]=>
string(4) "4.60"
[3]=>
string(4) "4.60"
}

PDO::ATTR_EMULATE_PREPARES = false, PDO::ATTR_STRINGIFY_FETCHES = true
array(8) {
["id"]=>
string(1) "1"
[0]=>
string(1) "1"
["float_col"]=>
string(3) "2.6"
[1]=>
string(3) "2.6"
["double_col"]=>
string(3) "3.6"
[2]=>
string(3) "3.6"
["decimal_col"]=>
string(4) "4.60"
[3]=>
string(4) "4.60"
}

PDO::ATTR_EMULATE_PREPARES = false, PDO::ATTR_STRINGIFY_FETCHES = false
array(8) {
["id"]=>
int(1)
[0]=>
int(1)
["float_col"]=>
float(2.6)
[1]=>
float(2.6)
["double_col"]=>
float(3.6)
[2]=>
float(3.6)
["decimal_col"]=>
string(4) "4.60"
[3]=>
string(4) "4.60"
}

done!