1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
<?php
/**
* Tests the drivers' various fetch methods
*
* Executed by driver/02fetch.phpt
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Database
* @package DB
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version $Id: fetchmodes.inc,v 1.14 2007/09/21 15:14:26 aharvey Exp $
* @link http://pear.php.net/package/DB
*/
/**
* Local error callback handler
*
* Drops the phptest table, prints out an error message and kills the
* process.
*
* @param object $o PEAR error object automatically passed to this method
* @return void
* @see PEAR::setErrorHandling()
*/
function pe($o) {
global $dbh;
$dbh->setErrorHandling(PEAR_ERROR_RETURN);
drop_table($dbh, 'phptest');
die($o->toString());
}
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
$dbh->setOption('autofree', true);
$dbh->query("INSERT INTO phptest VALUES (1, 'one', 'One', '2001-02-16')");
$dbh->query("INSERT INTO phptest VALUES (2, 'two', 'Two', '2001-02-15')");
$dbh->query("INSERT INTO phptest VALUES (3, 'three', 'Three', '2001-02-14')");
print "testing fetchrow:\n";
$sth = $dbh->query("SELECT * FROM phptest");
for ($i = 1; $i <= 5; $i++) {
print "row $i: ";
$row = $sth->fetchRow();
if (DB::isError($row)) {
print $row->toString() . "\n";
continue;
}
if (is_array($row)) {
print implode(', ', $row) . "\n";
} else {
var_dump($row);
}
}
$sth->free(); // keep fbsql happy.
$dbh->query('DELETE FROM phptest WHERE a <> 42');
print "testing fetchmodes: fetchrow default default, portability mode DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM\n";
$dbh->setOption('portability', DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM);
$sth = $dbh->query("SELECT * FROM phptest");
$row = $sth->fetchRow();
print implode(" ", array_keys($row))."\n";
$actual = implode(' ', array_values($row));
switch ($dbh->phptype) {
case 'fbsql':
case 'msql':
case 'mysql':
case 'mysqli':
case 'sqlite':
$expected = '42 bing This is a test 1999-11-21';
break;
case 'ifx':
$expected = '42 bing This is a test 1999-11-21 ';
break;
default:
$expected = '42 bing This is a test 1999-11-21';
}
if ($actual == $expected) {
echo "output matched expected format\n";
} else {
echo "DIDN'T MATCH! Expected output: '$expected'. Actual output: '$actual'.\n";
}
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchinto default default\n";
$dbh->setOption('portability', DB_PORTABILITY_ALL);
$sth = $dbh->query("SELECT * FROM phptest");
$row = array();
$sth->fetchInto($row);
print implode(" ", array_keys($row))."\n";
print implode(' ', array_values($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchrow ordered default\n";
$dbh->setFetchMode(DB_FETCHMODE_ORDERED);
$sth = $dbh->query("SELECT * FROM phptest");
$row = $sth->fetchRow();
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchrow assoc default\n";
$dbh->setFetchMode(DB_FETCHMODE_ASSOC);
$sth = $dbh->query("SELECT * FROM phptest");
$row = $sth->fetchRow();
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchrow ordered default with assoc specified\n";
$dbh->setFetchMode(DB_FETCHMODE_ORDERED);
$sth = $dbh->query("SELECT * FROM phptest");
$row = $sth->fetchRow(DB_FETCHMODE_ASSOC);
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchrow assoc default with ordered specified\n";
$dbh->setFetchMode(DB_FETCHMODE_ASSOC);
$sth = $dbh->query("SELECT * FROM phptest");
$row = $sth->fetchRow(DB_FETCHMODE_ORDERED);
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchinto ordered default\n";
$dbh->setFetchMode(DB_FETCHMODE_ORDERED);
$sth = $dbh->query("SELECT * FROM phptest");
$row = array();
$sth->fetchInto($row);
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchinto assoc default\n";
$dbh->setFetchMode(DB_FETCHMODE_ASSOC);
$sth = $dbh->query("SELECT * FROM phptest");
$row = array();
$sth->fetchInto($row);
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchinto ordered default with assoc specified\n";
$dbh->setFetchMode(DB_FETCHMODE_ORDERED);
$sth = $dbh->query("SELECT * FROM phptest");
$row = array();
$sth->fetchInto($row, DB_FETCHMODE_ASSOC);
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchinto assoc default with ordered specified\n";
$dbh->setFetchMode(DB_FETCHMODE_ASSOC);
$sth = $dbh->query("SELECT * FROM phptest");
$row = array();
$sth->fetchInto($row, DB_FETCHMODE_ORDERED);
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
print "testing fetchmodes: fetchrow assoc quoted identifiers\n";
if ($dbh->phptype == 'msql' || $dbh->phptype == 'ibase' || $dbh->phptype == 'oci8') {
// Some databases don't support quoted identifiers. Fake the output.
echo "a b cc d\n";
} else {
$dbh->setFetchMode(DB_FETCHMODE_ASSOC);
$sql = sprintf('SELECT %s, %s, %s, %s FROM %s',
$dbh->quoteIdentifier('a'),
$dbh->quoteIdentifier('b'),
$dbh->quoteIdentifier('cc'),
$dbh->quoteIdentifier('d'),
$dbh->quoteIdentifier('phptest'));
$sth = $dbh->query($sql);
$row = $sth->fetchRow();
print implode(" ", array_keys($row))."\n";
$sth->free(); // keep fbsql happy.
// keep ibase happy: can't drop tbl that has results open against it.
}
$dbh->setErrorHandling(PEAR_ERROR_RETURN);
drop_table($dbh, 'phptest');
|