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