1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
--TEST--
MySQL PDO:query() vs. PDO::prepare() and MySQL error 2050
--SKIPIF--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
$db = MySQLPDOTest::factory();
if (MYSQLPDOTest::isPDOMySQLnd())
die("skip libmysql only test");
?>
--FILE--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$db = MySQLPDOTest::factory();
try {
printf("Native PS...\n");
$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[004] Unable to turn off emulated prepared statements\n");
printf("Buffered...\n");
MySQLPDOTest::createTestTable($db);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$stmt = $db->query('SELECT id, label FROM test WHERE id = 1');
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
$stmt = $db->query('SELECT id, label FROM test WHERE id = 1');
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
printf("Unbuffered...\n");
MySQLPDOTest::createTestTable($db);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$stmt = $db->query('SELECT id, label FROM test WHERE id = 1');
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
/*
NOTE - this will cause an error and it OK
When using unbuffered prepared statements MySQL expects you to
fetch all data from the row before sending new data to the server.
PDO::query() will prepare and execute a statement in one step.
After the execution of PDO::query(), MySQL expects you to fetch
the results from the line before sending new commands. However,
PHP/PDO will send a CLOSE message as part of the PDO::query() call.
The following happens:
$stmt = PDO::query(<some query>)
mysql_stmt_prepare()
mysql_stmt_execute()
$stmt->fetchAll()
mysql_stmt_fetch()
And now the right side of the expression will be executed first:
$stmt = PDO::query(<some query>)
PDO::query(<some query>)
mysql_stmt_prepare
mysql_stmt_execute
PHP continues at the left side of the expression:
$stmt = PDO::query(<some query>)
What happens is that $stmt gets overwritten. The reference counter of the
zval representing the current value of $stmt. PDO gets a callback that
it has to free the resources associated with the zval representing the
current value of stmt:
mysql_stmt_close
---> ERROR
---> execute() has been send on the line, you are supposed to fetch
---> you must not try to send a CLOSE after execute()
---> Error: 2050 (CR_FETCH_CANCELED)
---> Message: Row retrieval was canceled by mysql_stmt_close() call
---> MySQL does its best to recover the line and cancels the retrieval
PHP proceeds and assigns the new statement object/zval obtained from
PDO to $stmt.
Solutions:
- use mysqlnd
- use prepare() + execute() instead of query()
- as there is no explicit close() in PDO, try unset($stmt) before the new assignment
- fix PDO::query() [not the driver, fix PDO itself]
*/
$stmt = $db->query('SELECT id, label FROM test WHERE id = 1');
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
$stmt = $db->prepare('SELECT id, label FROM test WHERE id = 1');
$stmt->execute();
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
$stmt = $db->prepare('SELECT id, label FROM test WHERE id = 1');
$stmt->execute();
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
unset($stmt);
$stmt = $db->query('SELECT id, label FROM test WHERE id = 1');
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
unset($stmt);
$stmt = $db->query('SELECT id, label FROM test WHERE id = 1');
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
} catch (PDOException $e) {
printf("[001] %s [%s] %s\n",
$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
}
print "done!";
?>
--CLEAN--
<?php
require dirname(__FILE__) . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
?>
--EXPECTF--
Native PS...
Buffered...
array(1) {
[0]=>
array(2) {
[%u|b%"id"]=>
%unicode|string%(1) "1"
[%u|b%"label"]=>
%unicode|string%(1) "a"
}
}
array(1) {
[0]=>
array(2) {
[%u|b%"id"]=>
%unicode|string%(1) "1"
[%u|b%"label"]=>
%unicode|string%(1) "a"
}
}
Unbuffered...
Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: 2050 in %s on line %d
array(0) {
}
Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: 2050 in %s on line %d
array(0) {
}
array(1) {
[0]=>
array(2) {
[%u|b%"id"]=>
%unicode|string%(1) "1"
[%u|b%"label"]=>
%unicode|string%(1) "a"
}
}
array(1) {
[0]=>
array(2) {
[%u|b%"id"]=>
%unicode|string%(1) "1"
[%u|b%"label"]=>
%unicode|string%(1) "a"
}
}
array(1) {
[0]=>
array(2) {
[%u|b%"id"]=>
%unicode|string%(1) "1"
[%u|b%"label"]=>
%unicode|string%(1) "a"
}
}
array(1) {
[0]=>
array(2) {
[%u|b%"id"]=>
%unicode|string%(1) "1"
[%u|b%"label"]=>
%unicode|string%(1) "a"
}
}
done!
|