File: 17query.phpt

package info (click to toggle)
php-db 1.7.13-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 872 kB
  • ctags: 1,600
  • sloc: php: 6,913; pascal: 1,001; xml: 198; makefile: 55; sh: 24
file content (170 lines) | stat: -rw-r--r-- 4,903 bytes parent folder | download
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
--TEST--
DB_driver::query
--INI--
error_reporting = 2047
--SKIPIF--
<?php

/**
 * Calls the query() method in various ways against any DBMS.
 *
 * @see      DB_common::query()
 * 
 * @package  DB
 * @version  $Id: 17query.phpt,v 1.19 2007/09/21 15:14:26 aharvey Exp $
 * @category Database
 * @author   Daniel Convissor <danielc@analysisandsolutions.com>
 * @internal
 */

chdir(dirname(__FILE__));
require_once './skipif.inc';

?>
--FILE--
<?php

// $Id: 17query.phpt,v 1.19 2007/09/21 15:14:26 aharvey Exp $

/**
 * Connect to the database and make the phptest table.
 */
require_once './mktable.inc';


/**
 * 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->setFetchMode(DB_FETCHMODE_ASSOC);


$res =& $dbh->query('DELETE FROM phptest WHERE a = 17');
print '1) delete: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";

$res =& $dbh->query("INSERT INTO phptest (a, b, cc) VALUES (17, 'one', 'One')");
print '2) insert: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";

$res =& $dbh->query('INSERT INTO phptest (a, b, cc) VALUES (?, ?, ?)', array(17, 'two', 'Two'));
print '3) insert: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";


$res =& $dbh->query('SELECT a, b FROM phptest WHERE a = 17');
$row = $res->fetchRow();
print "4) a = {$row['a']}, b = {$row['b']}\n";
$res->free();  // keep fbsql happy.

$res =& $dbh->query('SELECT a, b FROM phptest WHERE cc = ?', array('Two'));
$row = $res->fetchRow();
print "5) a = {$row['a']}, b = {$row['b']}\n";


$array = array(
    'foo' => 11,
    'bar' => 'three',
    'baz' => null,
);
$res =& $dbh->query('INSERT INTO phptest (a, b, d) VALUES (?, ?, ?)', $array);
print '6) insert: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";

$res =& $dbh->query('SELECT a, b, d FROM phptest WHERE a = ?', 11);
$row = $res->fetchRow();
print "7) a = {$row['a']}, b = {$row['b']}, d = ";
if ($dbh->phptype == 'msql') {
    if (array_key_exists('d', $row)) {
        $type = gettype($row['d']);
        if ($type == 'NULL' || $row['d'] == '') {
            print "got expected value\n";
        } else {
            print "ERR: expected d's type to be NULL but it's $type and the value is ";
            print $row['d'] . "\n";
        }
    } else {
        // http://bugs.php.net/?id=31960
        print "Prior to PHP 4.3.11 or 5.0.4, PHP's msql extension silently"
              . " dropped columns with null values. You need to upgrade.\n";
    }
} else {
    $type = gettype($row['d']);
    if ($type == 'NULL' || $row['d'] == '') {
        print "got expected value\n";
    } else {
        print "ERR: expected d's type to be NULL but it's $type and the value is ";
        print $row['d'] . "\n";
    }
}


$res =& $dbh->query('DELETE FROM phptest WHERE a = ?', array(17));
print '8) delete: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";

$res =& $dbh->query('DELETE FROM phptest WHERE a = ?', array(0));
print '9) delete with array(0) as param: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";

$res =& $dbh->query('DELETE FROM phptest WHERE a = ?', 0);
print '10) delete with 0 as param: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";

$dbh->nextQueryIsManip(true);
$res =& $dbh->query('SELECT * FROM phptest');
print '11) query is manip (with override): ' . ($dbh->_last_query_manip ? 'true' : 'false') . "\n";

$dbh->nextQueryIsManip(false);
$res =& $dbh->query('SELECT * FROM phptest');
print '12) query is manip (without override): ' . ($dbh->_last_query_manip ? 'true' : 'false') . "\n";

// This one's here for bug #11716.
if ($dbh->phptype == 'msql' || $dbh->phptype == 'ibase' || $dbh->phptype == 'oci8') {
    // Some databases don't support quoted identifiers. They are full of lose.
    $res =& $dbh->query('SELECT a FROM phptest');
} else {
    $res =& $dbh->query('SELECT '.$dbh->quoteIdentifier('a').' FROM phptest');
}

print '13) select with quoteIdentifier: ';
$row = $res->fetchRow(DB_FETCHMODE_ASSOC);
if (isset($row['a'])) {
    if ($row['a'] == 42) {
        print "okay\n";
    } else {
        print "field value incorrect\n";
    }
} else {
    print "expected field not in row\n";
}

$dbh->setErrorHandling(PEAR_ERROR_RETURN);
drop_table($dbh, 'phptest');

?>
--EXPECT--
1) delete: okay
2) insert: okay
3) insert: okay
4) a = 17, b = one
5) a = 17, b = two
6) insert: okay
7) a = 11, b = three, d = got expected value
8) delete: okay
9) delete with array(0) as param: okay
10) delete with 0 as param: okay
11) query is manip (with override): true
12) query is manip (without override): false
13) select with quoteIdentifier: okay