1--TEST-- 2MySQL PDOStatement->bindValue() 3--SKIPIF-- 4<?php 5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc'); 6require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 7MySQLPDOTest::skip(); 8$db = MySQLPDOTest::factory(); 9?> 10--FILE-- 11<?php 12 require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 13 $db = MySQLPDOTest::factory(); 14 MySQLPDOTest::createTestTable($db); 15 16 printf("Testing native PS...\n"); 17 try { 18 $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0); 19 if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 20 printf("[002] Unable to turn off emulated prepared statements\n"); 21 22 printf("Binding variable...\n"); 23 $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2'); 24 $in = 0; 25 if (!$stmt->bindValue(1, $in)) 26 printf("[003] Cannot bind value, %s %s\n", 27 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 28 29 $stmt->execute(); 30 $id = $label = null; 31 32 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 33 printf("[004] Cannot bind integer column, %s %s\n", 34 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 35 36 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 37 printf("[005] Cannot bind string column, %s %s\n", 38 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 39 40 while ($stmt->fetch(PDO::FETCH_BOUND)) 41 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 42 $in, 43 var_export($id, true), gettype($id), 44 var_export($label, true), gettype($label)); 45 46 printf("Binding value and not variable...\n"); 47 if (!$stmt->bindValue(1, 0)) 48 printf("[006] Cannot bind value, %s %s\n", 49 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 50 51 $stmt->execute(); 52 $id = $label = null; 53 54 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 55 printf("[007] Cannot bind integer column, %s %s\n", 56 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 57 58 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 59 printf("[008] Cannot bind string column, %s %s\n", 60 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 61 62 while ($stmt->fetch(PDO::FETCH_BOUND)) 63 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 64 $in, 65 var_export($id, true), gettype($id), 66 var_export($label, true), gettype($label)); 67 68 printf("Binding variable which references another variable...\n"); 69 $in = 0; 70 $in_ref = &$in; 71 if (!$stmt->bindValue(1, $in_ref)) 72 printf("[009] Cannot bind value, %s %s\n", 73 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 74 75 $stmt->execute(); 76 $id = $label = null; 77 78 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 79 printf("[010] Cannot bind integer column, %s %s\n", 80 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 81 82 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 83 printf("[011] Cannot bind string column, %s %s\n", 84 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 85 86 while ($stmt->fetch(PDO::FETCH_BOUND)) 87 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 88 $in, 89 var_export($id, true), gettype($id), 90 var_export($label, true), gettype($label)); 91 92 93 printf("Binding a variable and a value...\n"); 94 $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2'); 95 $in = 0; 96 if (!$stmt->bindValue(1, $in)) 97 printf("[012] Cannot bind value, %s %s\n", 98 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 99 100 if (!$stmt->bindValue(2, 2)) 101 printf("[013] Cannot bind value, %s %s\n", 102 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 103 104 $stmt->execute(); 105 $id = $label = null; 106 107 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 108 printf("[014] Cannot bind integer column, %s %s\n", 109 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 110 111 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 112 printf("[015] Cannot bind string column, %s %s\n", 113 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 114 115 while ($stmt->fetch(PDO::FETCH_BOUND)) 116 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 117 $in, 118 var_export($id, true), gettype($id), 119 var_export($label, true), gettype($label)); 120 121 printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n"); 122 // variable value change shall have no impact 123 $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2'); 124 $in = 0; 125 if (!$stmt->bindValue(1, $in)) 126 printf("[016] Cannot bind value, %s %s\n", 127 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 128 129 $in = 2; 130 if (!$stmt->bindValue(2, $in)) 131 printf("[017] Cannot bind value, %s %s\n", 132 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 133 134 $stmt->execute(); 135 $id = $label = null; 136 137 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 138 printf("[018] Cannot bind integer column, %s %s\n", 139 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 140 141 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 142 printf("[019] Cannot bind string column, %s %s\n", 143 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 144 145 while ($stmt->fetch(PDO::FETCH_BOUND)) 146 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 147 $in, 148 var_export($id, true), gettype($id), 149 var_export($label, true), gettype($label)); 150 151 } catch (PDOException $e) { 152 printf("[001] %s [%s] %s\n", 153 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 154 } 155 156 printf("Testing emulated PS...\n"); 157 try { 158 $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1); 159 if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 160 printf("[002] Unable to turn on emulated prepared statements\n"); 161 162 printf("Binding variable...\n"); 163 $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2'); 164 $in = 0; 165 if (!$stmt->bindValue(1, $in)) 166 printf("[003] Cannot bind value, %s %s\n", 167 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 168 169 $stmt->execute(); 170 $id = $label = null; 171 172 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 173 printf("[004] Cannot bind integer column, %s %s\n", 174 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 175 176 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 177 printf("[005] Cannot bind string column, %s %s\n", 178 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 179 180 while ($stmt->fetch(PDO::FETCH_BOUND)) 181 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 182 $in, 183 var_export($id, true), gettype($id), 184 var_export($label, true), gettype($label)); 185 186 printf("Binding value and not variable...\n"); 187 if (!$stmt->bindValue(1, 0)) 188 printf("[006] Cannot bind value, %s %s\n", 189 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 190 191 $stmt->execute(); 192 $id = $label = null; 193 194 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 195 printf("[007] Cannot bind integer column, %s %s\n", 196 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 197 198 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 199 printf("[008] Cannot bind string column, %s %s\n", 200 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 201 202 while ($stmt->fetch(PDO::FETCH_BOUND)) 203 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 204 $in, 205 var_export($id, true), gettype($id), 206 var_export($label, true), gettype($label)); 207 208 printf("Binding variable which references another variable...\n"); 209 $in = 0; 210 $in_ref = &$in; 211 if (!$stmt->bindValue(1, $in_ref)) 212 printf("[009] Cannot bind value, %s %s\n", 213 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 214 215 $stmt->execute(); 216 $id = $label = null; 217 218 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 219 printf("[010] Cannot bind integer column, %s %s\n", 220 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 221 222 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 223 printf("[011] Cannot bind string column, %s %s\n", 224 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 225 226 while ($stmt->fetch(PDO::FETCH_BOUND)) 227 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 228 $in, 229 var_export($id, true), gettype($id), 230 var_export($label, true), gettype($label)); 231 232 233 printf("Binding a variable and a value...\n"); 234 $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2'); 235 $in = 0; 236 if (!$stmt->bindValue(1, $in)) 237 printf("[012] Cannot bind value, %s %s\n", 238 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 239 240 if (!$stmt->bindValue(2, 2)) 241 printf("[013] Cannot bind value, %s %s\n", 242 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 243 244 $stmt->execute(); 245 $id = $label = null; 246 247 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 248 printf("[014] Cannot bind integer column, %s %s\n", 249 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 250 251 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 252 printf("[015] Cannot bind string column, %s %s\n", 253 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 254 255 while ($stmt->fetch(PDO::FETCH_BOUND)) 256 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 257 $in, 258 var_export($id, true), gettype($id), 259 var_export($label, true), gettype($label)); 260 261 printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n"); 262 // variable value change shall have no impact 263 $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2'); 264 $in = 0; 265 if (!$stmt->bindValue(1, $in)) 266 printf("[016] Cannot bind value, %s %s\n", 267 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 268 269 $in = 2; 270 if (!$stmt->bindValue(2, $in)) 271 printf("[017] Cannot bind value, %s %s\n", 272 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 273 274 $stmt->execute(); 275 $id = $label = null; 276 277 if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT)) 278 printf("[018] Cannot bind integer column, %s %s\n", 279 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 280 281 if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR)) 282 printf("[019] Cannot bind string column, %s %s\n", 283 $stmt->errorCode(), var_export($stmt->errorInfo(), true)); 284 285 while ($stmt->fetch(PDO::FETCH_BOUND)) 286 printf("in = %d -> id = %s (%s) / label = %s (%s)\n", 287 $in, 288 var_export($id, true), gettype($id), 289 var_export($label, true), gettype($label)); 290 291 } catch (PDOException $e) { 292 printf("[001] %s [%s] %s\n", 293 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 294 } 295 296 print "done!"; 297?> 298--CLEAN-- 299<?php 300require dirname(__FILE__) . '/mysql_pdo_test.inc'; 301MySQLPDOTest::dropTestTable(); 302?> 303--EXPECT-- 304Testing native PS... 305Binding variable... 306in = 0 -> id = 1 (integer) / label = 'a' (string) 307in = 0 -> id = 2 (integer) / label = 'b' (string) 308Binding value and not variable... 309in = 0 -> id = 1 (integer) / label = 'a' (string) 310in = 0 -> id = 2 (integer) / label = 'b' (string) 311Binding variable which references another variable... 312in = 0 -> id = 1 (integer) / label = 'a' (string) 313in = 0 -> id = 2 (integer) / label = 'b' (string) 314Binding a variable and a value... 315in = 0 -> id = 1 (integer) / label = 'a' (string) 316in = 0 -> id = 2 (integer) / label = 'b' (string) 317Binding a variable to two placeholders and changing the variable value in between the binds... 318in = 2 -> id = 1 (integer) / label = 'a' (string) 319in = 2 -> id = 2 (integer) / label = 'b' (string) 320Testing emulated PS... 321Binding variable... 322in = 0 -> id = 1 (integer) / label = 'a' (string) 323in = 0 -> id = 2 (integer) / label = 'b' (string) 324Binding value and not variable... 325in = 0 -> id = 1 (integer) / label = 'a' (string) 326in = 0 -> id = 2 (integer) / label = 'b' (string) 327Binding variable which references another variable... 328in = 0 -> id = 1 (integer) / label = 'a' (string) 329in = 0 -> id = 2 (integer) / label = 'b' (string) 330Binding a variable and a value... 331in = 0 -> id = 1 (integer) / label = 'a' (string) 332in = 0 -> id = 2 (integer) / label = 'b' (string) 333Binding a variable to two placeholders and changing the variable value in between the binds... 334in = 2 -> id = 1 (integer) / label = 'a' (string) 335in = 2 -> id = 2 (integer) / label = 'b' (string) 336done! 337