Skip to content
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

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

Closed
Show file tree
Hide file tree
Changes from 4 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
16 changes: 14 additions & 2 deletions ext/pdo_mysql/mysql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,19 @@ static bool pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
((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)) {
return false;
Girgias marked this conversation as resolved.
Show resolved Hide resolved
}
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);
return false;
Girgias marked this conversation as resolved.
Show resolved Hide resolved
}
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
149 changes: 149 additions & 0 deletions ext/pdo_mysql/tests/gh-11587.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
--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
if (!extension_loaded('mysqli') || !extension_loaded('mysqlnd')) {
/* Need connection to detect library version */
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
}
?>
--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);

// PDO::ATTR_EMULATE_PREPARES = true, PDO::ATTR_STRINGIFY_FETCHES = true
Girgias marked this conversation as resolved.
Show resolved Hide resolved
$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);
}

// PDO::ATTR_EMULATE_PREPARES = true, PDO::ATTR_STRINGIFY_FETCHES = false
$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);
}

// PDO::ATTR_EMULATE_PREPARES = false, PDO::ATTR_STRINGIFY_FETCHES = true
$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);
}

// PDO::ATTR_EMULATE_PREPARES = false, PDO::ATTR_STRINGIFY_FETCHES = false
$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 'done!';
?>
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
?>
--EXPECT--
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"
}
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"
}
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"
}
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!