<?php if (getenv('REDIR_TEST_DIR') === false) { putenv('REDIR_TEST_DIR=' . dirname(__FILE__) . '/../../pdo/tests/'); } require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; $pdo = PDOTest::factory(); $pdo->exec("create table test (id integer primary key, n varchar(255))"); $pdo->exec("INSERT INTO test (id, n) VALUES (1, 'hi')"); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS); $stmt = $pdo->prepare("SELECT * FROM test"); $stmt->execute(); var_dump($stmt->fetchAll()); if ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == 'oci') { $type = "clob"; } else { if ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') { $type = 'BLOB SUB_TYPE TEXT'; } else { $type = "text"; } } $pdo->exec("create table test2 (id integer primary key, n {$type})"); $pdo->exec("INSERT INTO test2 (id, n) VALUES (1,'hi')"); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_FUNC); $stmt = $pdo->prepare("SELECT * FROM test2"); $stmt->execute(); var_dump($stmt->fetchAll());
<?php if (getenv('REDIR_TEST_DIR') === false) { putenv('REDIR_TEST_DIR=' . dirname(__FILE__) . '/../../pdo/tests/'); } require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; $db = PDOTest::factory(); $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(20))'); $db->exec('INSERT INTO test VALUES(1, \'A\', \'A2\')'); $db->exec('INSERT INTO test VALUES(2, \'A\', \'B2\')'); $select1 = $db->prepare('SELECT id, val, val2 FROM test'); $select2 = $db->prepare('SELECT val, val2 FROM test'); $select1->execute(); var_dump($select1->fetchAll(PDO::FETCH_COLUMN)); $select1->execute(); var_dump($select1->fetchAll(PDO::FETCH_COLUMN, 2)); $select1->execute(); var_dump($select1->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP)); $select1->execute(); var_dump($select1->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE)); $select1->execute(); var_dump($select1->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE, 0)); $select1->execute(); var_dump($select1->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE, 1)); $select1->execute(); var_dump($select1->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE, 2)); $select2->execute(); var_dump($select2->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP));
class PDODatabase extends PDO { function __destruct() { echo __METHOD__ . "()\n"; } function query($sql) { echo __METHOD__ . "()\n"; $stmt = $this->prepare($sql, array(PDO::ATTR_STATEMENT_CLASS => array('PDOStatementx', array($this)))); $stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(); return $stmt; } } $db = PDOTest::factory('PDODatabase'); var_dump(get_class($db)); $db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))'); $stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)"); var_dump(get_class($stmt)); foreach ($data as $row) { $stmt->execute($row); } unset($stmt); $stmt = $db->query('SELECT * FROM test'); var_dump(get_class($stmt)); var_dump(get_class($stmt->dbh)); foreach ($stmt as $obj) { var_dump($obj); } echo "===DONE===\n";