function pdo_mysql_errorinfo($db, $offset) { try { /* If you create a PDOStatement object through PDO->prepare() or PDO->query() and invoke an error on the statement handle, PDO->errorCode() will not reflect that error. You must call PDOStatement->errorCode() to return the error code for an operation performed on a particular statement handle. */ $code = $db->errorCode(); check_error($offset + 2, $db); $stmt = $db->query('SELECT id, label FROM test'); $stmt2 =& $stmt; check_error($offset + 3, $db); check_error($offset + 4, $stmt); $db->exec('DROP TABLE IF EXISTS test'); @$stmt->execute(); check_error($offset + 5, $db); check_error($offset + 6, $stmt, '42S02'); check_error($offset + 7, $stmt2, '42S02'); @($stmt = $db->query('SELECT id, label FROM unknown')); check_error($offset + 8, $db, '42S02'); MySQLPDOTest::createTestTable($db); $stmt = $db->query('SELECT id, label FROM test'); check_error($offset + 9, $db); check_error($offset + 10, $stmt); $db2 =& $db; $db->exec('DROP TABLE IF EXISTS unknown'); @$db->query('SELECT id, label FROM unknown'); check_error($offset + 11, $db, '42S02'); check_error($offset + 12, $db2, '42S02'); check_error($offset + 13, $stmt); check_error($offset + 14, $stmt2); // lets hope this is an invalid attribute code $invalid_attr = -1 * PHP_INT_MAX + 3; $tmp = @$db->getAttribute($invalid_attr); check_error($offset + 15, $db, 'IM001'); check_error($offset + 16, $db2, 'IM001'); check_error($offset + 17, $stmt); check_error($offset + 18, $stmt2); } catch (PDOException $e) { printf("[%03d] %s [%s] %s\n", $offset + 19, $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); } }
<?php require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'; $db = MySQLPDOTest::factory(); MySQLPDOTest::createTestTable($db); function pdo_mysql_stmt_bindparam_types_do($db, $offset, $native, $sql_type, $value) { if ($native) { $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0); } else { $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1); } $db->exec('DROP TABLE IF EXISTS test'); $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine()); if (!($stmt = @$db->prepare($sql)) || !@$stmt->execute()) { // Server might not support column type - skip it return true; } $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (1, ?)'); if (!$stmt->bindParam(1, $value)) { printf("[%03d/%s + 1] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true)); return false; } if (!$stmt->execute()) { printf("[%03d/%s + 2] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true)); return false; } $stmt = $db->query('SELECT id, label FROM test'); $id = $label = null; if (!$stmt->bindColumn(1, $id)) { printf("[%03d/%s + 3] %s\n", $offset, $native ? 'native' : 'emulated', var_export($stmt->errorInfo(), true));
<?php require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'; $db = MySQLPDOTest::factory(); MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db)); function find_invalid_int($valid_options) { do { $invalid = mt_rand(-10000, 10000); } while (in_array($invalid, $valid_options)); return $invalid; } function set_and_get($offset, $db, $attribute, $value) { $value_type = gettype($value); try { if (!$db->setAttribute($attribute, $value)) { printf("[%03d] Cannot set attribute '%s' to value '%s'\n", $offset, $attribute, var_export($tmp, true)); return false; } if (gettype($value) != $value_type) { printf("[%03d] Call to PDO::setAttribute(int attribute, mixed value) has changed the type of value from %s to %s, test will not work properly\n", $offset, $value_type, gettype($value)); return false; } $tmp = $db->getAttribute($attribute); if ($tmp !== $value) { printf("[%03d] Attribute '%s' was set to '%s'/%s but getAttribute() reports '%s'/%s\n", $offset, $attribute, var_export($value, true), gettype($value), var_export($tmp, true), gettype($tmp)); return false; } } catch (PDOException $e) { printf("[%03d] %s, [%s] %s\n", $offset, $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
} $db->exec("INSERT INTO test(id, label) VALUES (100, 'z')"); if (true !== ($tmp = $db->commit())) { printf("[005] No commit allowed? [%s] %s\n", $db->errorCode(), implode(' ', $db->errorInfo())); } // a weak test without unicode etc. - lets leave that to dedicated tests $stmt = $db->query('SELECT id, label FROM test WHERE id = 100'); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!isset($rows[0]['label']) || $rows[0]['label'] != 'z') { printf("[006] Record data is strange, dumping rows\n"); var_dump($rows); } // Ok, lets check MyISAM resp. any other non-transactional engine // pdo_mysql_begin_transaction has more on this, quick check only $db = MySQLPDOTest::factory(); MySQLPDOTest::createTestTable($db, 'MyISAM'); if (true !== ($tmp = $db->beginTransaction())) { printf("[007] Expecting true, got %s/%s\n", gettype($tmp), $tmp); } $db->exec("INSERT INTO test(id, label) VALUES (100, 'z')"); if (true !== ($tmp = $db->commit())) { printf("[008] No commit allowed? [%s] %s\n", $db->errorCode(), implode(' ', $db->errorInfo())); } // a weak test without unicode etc. - lets leave that to dedicated tests $stmt = $db->query('SELECT id, label FROM test WHERE id = 100'); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); if (!isset($rows[0]['label']) || $rows[0]['label'] != 'z') { printf("[009] Record data is strange, dumping rows\n"); var_dump($rows); } } catch (PDOException $e) {