1--TEST-- 2MySQL PDO->prepare(), native PS 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 function prepex($offset, &$db, $query, $input_params = null, $error_info = null, $suppress_warning = false) { 16 try { 17 if ($suppress_warning || (is_array($error_info) && isset($error_info['prepare']))) 18 $stmt = @$db->prepare($query); 19 else 20 $stmt = $db->prepare($query); 21 22 if (is_array($error_info) && isset($error_info['prepare'])) { 23 $tmp = $db->errorInfo(); 24 25 if (isset($error_info['prepare']['sqlstate']) && 26 ($error_info['prepare']['sqlstate'] !== $tmp[0])) { 27 printf("[%03d] prepare() - expecting SQLSTATE '%s' got '%s'\n", 28 $offset, $error_info['prepare']['sqlstate'], $tmp[0]); 29 return false; 30 } 31 32 if (isset($error_info['prepare']['mysql']) && 33 ($error_info['prepare']['mysql'] !== $tmp[1])) { 34 printf("[%03d] prepare() - expecting MySQL Code '%s' got '%s'\n", 35 $offset, $error_info['prepare']['mysql'], $tmp[0]); 36 return false; 37 } 38 39 return false; 40 } 41 42 if (!is_object($stmt)) 43 return false; 44 45 if (is_null($input_params)) 46 $input_params = array(); 47// 5.0.18, 5.1.14 @ 15 48// printf("[%03d]\n", $offset); 49 if ($suppress_warning || (is_array($error_info) && isset($error_info['execute']))) 50 $ret = @$stmt->execute($input_params); 51 else 52 $ret = $stmt->execute($input_params); 53 54 if (!is_bool($ret)) 55 printf("[%03d] PDO::execute() should return a boolean value, got %s/%s\n", 56 var_export($ret, true), $ret); 57 58 $tmp = $stmt->errorInfo(); 59 if (isset($tmp[1]) && ($tmp[1] == 2030)) { 60 // Trying to hack around MySQL Server version dependent features 61 // 2030 This command is not supported in the prepared statement protocol yet 62 return false; 63 } 64 65 if (is_array($error_info) && isset($error_info['execute'])) { 66 67 if (isset($error_info['execute']['sqlstate']) && 68 ($error_info['execute']['sqlstate'] !== $tmp[0])) { 69 printf("[%03d] execute() - expecting SQLSTATE '%s' got '%s'\n", 70 $offset, $error_info['execute']['sqlstate'], $tmp[0]); 71 return false; 72 } 73 74 if (isset($error_info['execute']['mysql']) && 75 ($error_info['execute']['mysql'] !== $tmp[1])) { 76 printf("[%03d] execute() - expecting MySQL Code '%s' got '%s'\n", 77 $offset, $error_info['execute']['mysql'], $tmp[0]); 78 return false; 79 } 80 81 return false; 82 } 83 } catch (PDOException $e) { 84 printf("[%03d] %s, [%s} %s\n", 85 $offset, $e->getMessage(), 86 $db->errorCode(), implode(' ', $db->errorInfo())); 87 return false; 88 } 89 90 return $stmt; 91 } 92 93 try { 94 $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0); 95 if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 96 printf("[002] Unable to turn off emulated prepared statements\n"); 97 98 try { 99 prepex(3, $db, '', [], ['prepare' => ['sqlstate' => '42000']]); 100 } catch (\ValueError $e) { 101 echo $e->getMessage(), \PHP_EOL; 102 } 103 104 prepex(4, $db, 'CREATE TABLE test_prepare_native_myisam(id INT, label CHAR(255)) ENGINE=MyISAM'); 105 // Not every MySQL Server version supports this 106 if (is_object(prepex(5, $db, 'CREATE FULLTEXT INDEX idx1 ON test_prepare_native_myisam(label)', null, null, true))) { 107 prepex(6, $db, 'INSERT INTO test_prepare_native_myisam(id, label) VALUES (1, :placeholder)', 108 array(':placeholder' => 'MySQL is the best database in the world!')); 109 prepex(7, $db, 'INSERT INTO test_prepare_native_myisam(id, label) VALUES (2, :placeholder)', 110 array(':placeholder' => 'If I have the freedom to choose, I would always go again for the MySQL Server')); 111 $stmt = prepex(8, $db, 'SELECT id, label FROM test_prepare_native_myisam WHERE MATCH label AGAINST (:placeholder)', 112 array(':placeholder' => 'mysql'), null, true); 113 if (is_object($stmt)) { 114 /* 115 Lets ignore this 116 if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2) 117 printf("[033] Expecting two rows, got %d rows\n", $tmp); 118 */ 119 $stmt = prepex(9, $db, 'SELECT id, label FROM test_prepare_native_myisam ORDER BY id ASC'); 120 if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 2) 121 printf("[027] Expecting two rows, got %d rows\n", $tmp); 122 123 if ($tmp[0]['label'] !== 'MySQL is the best database in the world!') { 124 printf("[028] INSERT seems to have failed, dumping data, check manually\n"); 125 var_dump($tmp); 126 } 127 } 128 } 129 130 $db->exec('DELETE FROM test_prepare_native_myisam'); 131 $db->exec("INSERT INTO test_prepare_native_myisam(id, label) VALUES (1, 'row1'), (2, 'row2')"); 132 133 $sql = sprintf("SELECT id, label FROM test_prepare_native_myisam WHERE (label LIKE %s) AND (id = :placeholder)", 134 $db->quote('%ro%')); 135 $stmt = prepex(10, $db, $sql, array('placeholder' => -1)); 136 if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 0) 137 printf("[030] Expecting zero rows, got %d rows\n", $tmp); 138 139 $sql = sprintf("SELECT id, label FROM test_prepare_native_myisam WHERE (id = :placeholder) OR (label LIKE %s)", 140 $db->quote('%go%')); 141 $stmt = prepex(11, $db, $sql, array('placeholder' => 1)); 142 if (count(($tmp = $stmt->fetchAll(PDO::FETCH_ASSOC))) != 1) 143 printf("[032] Expecting one row, got %d rows\n", $tmp); 144 } catch (PDOException $e) { 145 printf("[001] %s [%s] %s\n", 146 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 147 } 148 149 print "done!"; 150?> 151--CLEAN-- 152<?php 153require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 154$db = MySQLPDOTest::factory(); 155$db->exec('DROP TABLE IF EXISTS test_prepare_native_myisam'); 156?> 157--EXPECT-- 158PDO::prepare(): Argument #1 ($query) must not be empty 159done! 160