1--TEST-- 2MySQL PDO->prepare(), emulated PS, anonymous placeholder 3--SKIPIF-- 4<?php 5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc'); 6require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 7MySQLPDOTest::skip(); 8$db = MySQLPDOTest::factory(); 9?> 10--FILE-- 11<?php 12 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 13 $db = MySQLPDOTest::factory(); 14 15 try { 16 $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1); 17 if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 18 printf("[002] Unable to switch to emulated prepared statements, test will fail\n"); 19 20 $db->exec('DROP TABLE IF EXISTS test'); 21 $db->exec(sprintf('CREATE TABLE test(id INT, label CHAR(255)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE)); 22 23 $stmt = $db->prepare("INSERT INTO test(id, label) VALUES(1, '?')"); 24 // you can bind as many values as you want no matter if they can be replaced or not 25 $stmt->execute(array('first row')); 26 if ('00000' !== $stmt->errorCode()) 27 printf("[003] Execute has failed, %s %s\n", 28 var_export($stmt->errorCode(), true), 29 var_export($stmt->errorInfo(), true)); 30 31 $stmt = $db->prepare('SELECT id, label FROM test'); 32 $stmt->execute(); 33 var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); 34 35 // now the same with native PS 36 printf("now the same with native PS\n"); 37 $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0); 38 if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 39 printf("[004] Unable to switch off emulated prepared statements, test will fail\n"); 40 41 $db->exec('DELETE FROM test'); 42 $stmt = $db->prepare("INSERT INTO test(id, label) VALUES(1, '?')"); 43 // you can bind as many values as you want no matter if they can be replaced or not 44 $stmt->execute(array('first row')); 45 if ('00000' !== $stmt->errorCode()) 46 printf("[005] Execute has failed, %s %s\n", 47 var_export($stmt->errorCode(), true), 48 var_export($stmt->errorInfo(), true)); 49 50 $stmt = $db->prepare('SELECT id, label FROM test'); 51 $stmt->execute(); 52 var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); 53 54 } catch (PDOException $e) { 55 printf("[001] %s [%s] %s\n", 56 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 57 } 58 59 print "done!"; 60?> 61--CLEAN-- 62<?php 63require __DIR__ . '/mysql_pdo_test.inc'; 64$db = MySQLPDOTest::factory(); 65$db->exec('DROP TABLE IF EXISTS test'); 66?> 67--EXPECTF-- 68Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in %s on line %d 69[003] Execute has failed, 'HY093' array ( 70 0 => 'HY093', 71 1 => NULL, 72 2 => NULL, 73) 74array(0) { 75} 76now the same with native PS 77 78Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d 79[005] Execute has failed, 'HY093' array ( 80 0 => 'HY093', 81 1 => NULL, 82 2 => NULL, 83) 84array(0) { 85} 86done! 87