mirror of
https://github.com/php/php-src.git
synced 2025-08-17 22:48:57 +02:00

Previously, PDO MySQL only fetched data as native int/float if native prepared statements were used. This patch updates PDO to have the same behavior for emulated prepared statements, and thus removes the largest remaining discrepancy between these two modes. Note that PDO already has a ATTR_STRINGIFY_FETCHES option to control whether native types are desired or not. The previous output can be restored by enabling this option. Most of the tests make use of that option, because this allows the tests to work under libmysqlclient as well, which currently always returns string results (independently of whether native or emulated PS are used).
100 lines
2.8 KiB
PHP
100 lines
2.8 KiB
PHP
--TEST--
|
|
PDO::MYSQL_ATTR_MULTI_STATEMENTS
|
|
--SKIPIF--
|
|
<?php
|
|
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
|
|
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
|
|
MySQLPDOTest::skip();
|
|
$db = MySQLPDOTest::factory();
|
|
?>
|
|
--INI--
|
|
error_reporting=E_ALL
|
|
--FILE--
|
|
<?php
|
|
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
|
|
|
|
$dsn = MySQLPDOTest::getDSN();
|
|
$user = PDO_MYSQL_TEST_USER;
|
|
$pass = PDO_MYSQL_TEST_PASS;
|
|
|
|
$table = sprintf("test_%s", md5(mt_rand(0, PHP_INT_MAX)));
|
|
$db = new PDO($dsn, $user, $pass);
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
|
|
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
|
|
$db->exec(sprintf('DROP TABLE IF EXISTS %s', $table));
|
|
$create = sprintf('CREATE TABLE %s(id INT)', $table);
|
|
$db->exec($create);
|
|
$db->exec(sprintf('INSERT INTO %s(id) VALUES (1)', $table));
|
|
$stmt = $db->query(sprintf('SELECT * FROM %s; INSERT INTO %s(id) VALUES (2)', $table, $table));
|
|
$stmt->closeCursor();
|
|
$info = $db->errorInfo();
|
|
var_dump($info[0]);
|
|
$stmt = $db->query(sprintf('SELECT id FROM %s', $table));
|
|
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
|
|
// A single query with a trailing delimiter.
|
|
$stmt = $db->query('SELECT 1 AS value;');
|
|
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
|
|
|
|
// New connection, does not allow multiple statements.
|
|
$db = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_MULTI_STATEMENTS => false));
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
|
|
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
|
|
$stmt = $db->query(sprintf('SELECT * FROM %s; INSERT INTO %s(id) VALUES (3)', $table, $table));
|
|
var_dump($stmt);
|
|
$info = $db->errorInfo();
|
|
var_dump($info[0]);
|
|
|
|
$stmt = $db->query(sprintf('SELECT id FROM %s', $table));
|
|
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
|
|
// A single query with a trailing delimiter.
|
|
$stmt = $db->query('SELECT 1 AS value;');
|
|
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
|
|
|
|
$db->exec(sprintf('DROP TABLE IF EXISTS %s', $table));
|
|
print "done!";
|
|
?>
|
|
--EXPECTF--
|
|
string(5) "00000"
|
|
array(2) {
|
|
[0]=>
|
|
array(1) {
|
|
["id"]=>
|
|
string(1) "1"
|
|
}
|
|
[1]=>
|
|
array(1) {
|
|
["id"]=>
|
|
string(1) "2"
|
|
}
|
|
}
|
|
array(1) {
|
|
[0]=>
|
|
array(1) {
|
|
["value"]=>
|
|
string(1) "1"
|
|
}
|
|
}
|
|
|
|
Warning: PDO::query(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near 'INSERT INTO %s(id) VALUES (3)' at line 1 in %s on line %d
|
|
bool(false)
|
|
string(5) "42000"
|
|
array(2) {
|
|
[0]=>
|
|
array(1) {
|
|
["id"]=>
|
|
string(1) "1"
|
|
}
|
|
[1]=>
|
|
array(1) {
|
|
["id"]=>
|
|
string(1) "2"
|
|
}
|
|
}
|
|
array(1) {
|
|
[0]=>
|
|
array(1) {
|
|
["value"]=>
|
|
string(1) "1"
|
|
}
|
|
}
|
|
done!
|