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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
--TEST--
MySQL PDOStatement->bindValue()
--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();
?>
--FILE--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$db = MySQLPDOTest::factory();
MySQLPDOTest::createTestTable($db);
printf("Testing native PS...\n");
try {
$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[002] Unable to turn off emulated prepared statements\n");
printf("Binding variable...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[003] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[004] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[005] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding value and not variable...\n");
if (!$stmt->bindValue(1, 0))
printf("[006] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[007] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[008] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding variable which references another variable...\n");
$in = 0;
$in_ref = &$in;
if (!$stmt->bindValue(1, $in_ref))
printf("[009] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[010] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[011] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable and a value...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[012] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindValue(2, 2))
printf("[013] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[014] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[015] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n");
// variable value change shall have no impact
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[016] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$in = 2;
if (!$stmt->bindValue(2, $in))
printf("[017] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[018] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[019] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
} catch (PDOException $e) {
printf("[001] %s [%s] %s\n",
$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
}
printf("Testing emulated PS...\n");
try {
$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[002] Unable to turn on emulated prepared statements\n");
printf("Binding variable...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[003] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[004] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[005] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding value and not variable...\n");
if (!$stmt->bindValue(1, 0))
printf("[006] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[007] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[008] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding variable which references another variable...\n");
$in = 0;
$in_ref = &$in;
if (!$stmt->bindValue(1, $in_ref))
printf("[009] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[010] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[011] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable and a value...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[012] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindValue(2, 2))
printf("[013] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[014] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[015] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n");
// variable value change shall have no impact
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[016] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$in = 2;
if (!$stmt->bindValue(2, $in))
printf("[017] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[018] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[019] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
} 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--
Testing native PS...
Binding variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding value and not variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding variable which references another variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable and a value...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable to two placeholders and changing the variable value in between the binds...
in = 2 -> id = 1 (integer) / label = 'a' (string)
in = 2 -> id = 2 (integer) / label = 'b' (string)
Testing emulated PS...
Binding variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding value and not variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding variable which references another variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable and a value...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable to two placeholders and changing the variable value in between the binds...
in = 2 -> id = 1 (integer) / label = 'a' (string)
in = 2 -> id = 2 (integer) / label = 'b' (string)
done!
|