Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/**
4
 * Tests the drivers' prepare and execute methods
5
 *
6
 * Executed by driver/06prepexec.phpt
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * LICENSE: This source file is subject to version 3.0 of the PHP license
11
 * that is available through the world-wide-web at the following URI:
12
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13
 * the PHP License and are unable to obtain it through the web, please
14
 * send a note to license@php.net so we can mail you a copy immediately.
15
 *
16
 * @category   Database
17
 * @package    DB
18
 * @author     Daniel Convissor <danielc@php.net>
19
 * @copyright  1997-2005 The PHP Group
20
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
21
 * @version    $Id: prepexe.inc,v 1.25 2005/02/16 13:54:51 danielc Exp $
22
 * @link       http://pear.php.net/package/DB
23
 */
24
 
25
$tmpfile = tempnam("/tmp", "phptmp");
26
register_shutdown_function("my_shutdown");
27
$fp = fopen($tmpfile, "w");
28
$filedata = "opaque placeholder's test";
29
fwrite($fp, $filedata);
30
fclose($fp);
31
 
32
 
33
/**
34
 * Local error callback handler
35
 *
36
 * Prints out an error message and kills the process.
37
 *
38
 * @param object  $o  PEAR error object automatically passed to this method
39
 * @return void
40
 * @see PEAR::setErrorHandling()
41
 */
42
function pe($o) {
43
    print "\n" . $o->toString();
44
    exit;
45
}
46
 
47
$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
48
 
49
 
50
// 1) Multiple prepare/exec INSERT queries
51
echo "------------1------------\n";
52
 
53
$sth1 = $dbh->prepare("INSERT INTO phptest (a, b) VALUES (?, 'a')");
54
$sth2 = $dbh->prepare("INSERT INTO phptest (a,b) VALUES (!,?)");
55
$sth3 = $dbh->prepare("INSERT INTO phptest (a,b,c) VALUES (?,!,&)");
56
$sth4 = $dbh->prepare("INSERT INTO phptest (a, b) VALUES (72, 'direct')");
57
print "sth1,sth2,sth3,sth4 created\n";
58
print 'sth1: ? as param, passing as array... ';
59
if (($res = $dbh->execute($sth1, array(72))) === DB_OK) {
60
    print "sth1 executed\n";
61
} else {
62
    print "sth1 failed\n";
63
}
64
 
65
print 'sth2: ! and ? as params, passing as array... ';
66
if (($res = $dbh->execute($sth2, array(72, "that's right"))) === DB_OK) {
67
    print "sth2 executed\n";
68
} else {
69
    print "sth2 failed\n";
70
}
71
 
72
print 'sth3: ?, ! and & as params, passing as array... ';
73
switch ($dbh->phptype) {
74
    case 'msql':
75
        $res = $dbh->execute($sth3, array(72, "'it\\'s good'", $tmpfile));
76
        break;
77
    default:
78
        $res = $dbh->execute($sth3, array(72, "'it''s good'", $tmpfile));
79
}
80
if ($res === DB_OK) {
81
    print "sth3 executed\n";
82
} else {
83
    print "sth3 failed\n";
84
}
85
 
86
print 'sth4: no params... ';
87
if (($res = $dbh->execute($sth4)) === DB_OK) {
88
    print "sth4 executed\n";
89
} else {
90
    print "sth4 failed\n";
91
}
92
print_results();
93
 
94
 
95
// 2) One prepared, multiple time executed
96
echo "\n------------2------------\n";
97
 
98
$dbh->query('DELETE FROM phptest');
99
$sth = $dbh->prepare('INSERT INTO phptest (a, b, c, d) VALUES (?, ?, &, ?)');
100
$data = array(
101
 
102
    1 => array(72, 'set2', $tmpfile, null),
103
    2 => array(72, 'set3', $tmpfile, null)
104
);
105
$res = $dbh->executeMultiple($sth, $data);
106
print_results();
107
 
108
 
109
// 3) freePrepared() test
110
echo "\n------------3------------\n";
111
 
112
if ($dbh->freePrepared($sth)) {
113
    echo 'TRUE';
114
} else {
115
    echo 'FALSE';
116
}
117
echo "\n";
118
if ($dbh->freePrepared(666)) {
119
    echo 'TRUE';
120
} else {
121
    echo 'FALSE';
122
}
123
echo "\n";
124
 
125
 
126
// 4) SELECTs tests
127
echo "\n------------4------------\n";
128
$sth1 = $dbh->prepare("SELECT * FROM phptest WHERE a = ? ORDER BY b");
129
print_4($sth1, 72);
130
print_4($sth1, 71);
131
$sth2 = $dbh->prepare("SELECT * FROM phptest WHERE d = ? ORDER BY b");
132
print_4($sth2, '1234-56-78');
133
$sth3 = $dbh->prepare("SELECT * FROM phptest WHERE c = & ORDER BY b");
134
print_4($sth3, $tmpfile);
135
 
136
 
137
// 5) ASSOCIATIVE ARRAY queries
138
echo "\n------------5------------\n";
139
 
140
$sth5 = $dbh->prepare('INSERT INTO phptest (a, b, d) VALUES (?, ?, ?)');
141
$array = array(
142
    'foo' => 11,
143
    'bar' => 'three',
144
    'baz' => null,
145
);
146
$res = $dbh->execute($sth5, $array);
147
print 'insert: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";
148
 
149
$sth6 = $dbh->prepare('SELECT a, b, d FROM phptest WHERE a = ?');
150
$res = $dbh->execute($sth6, array(11));
151
$row = $res->fetchRow(DB_FETCHMODE_ASSOC);
152
print "a = {$row['a']}, b = {$row['b']}, d = ";
153
if ($dbh->phptype == 'msql') {
154
    if (array_key_exists('d', $row)) {
155
        $type = gettype($row['d']);
156
        if ($type == 'NULL' || $row['d'] == '') {
157
            print "got expected outcome\n";
158
        } else {
159
            $type = gettype($row['d']);
160
            print "UN-expected outcome: $type\n";
161
        }
162
    } else {
163
        // http://bugs.php.net/?id=31960
164
        print "Prior to PHP 4.3.11 or 5.0.4, PHP's msql extension silently"
165
              . " dropped columns with null values. You need to upgrade.\n";
166
    }
167
} else {
168
    $type = gettype($row['d']);
169
    if ($type == 'string') {
170
        print "got expected outcome\n";
171
    } else {
172
        print "UN-expected outcome: $type\n";
173
    }
174
}
175
 
176
/**
177
 * Automatically free the prepared statements and results when the script
178
 * terminates
179
 *
180
 * @return void
181
 */
182
function my_shutdown() {
183
    global $tmpfile, $dbh, $sth1, $sth2, $sth3, $sth4, $sth5, $sth6, $res;
184
 
185
    switch ($dbh->phptype) {
186
        case 'ibase':
187
            /*
188
             * Interbase doesn't allow dropping tables that have result
189
             * sets still open.
190
             */
191
            $dbh->freePrepared($sth1);
192
            $dbh->freePrepared($sth2);
193
            $dbh->freePrepared($sth3);
194
            $dbh->freePrepared($sth4);
195
            $dbh->freePrepared($sth5);
196
            $dbh->freePrepared($sth6);
197
            $dbh->freeResult($res->result);
198
            break;
199
    }
200
 
201
    $dbh->setErrorHandling(PEAR_ERROR_RETURN);
202
    drop_table($dbh, 'phptest');
203
 
204
    unlink($tmpfile);
205
}
206
 
207
/**
208
 * Print out the data in test table
209
 *
210
 * @return void
211
 */
212
function print_results() {
213
    global $dbh;
214
    print "results:\n";
215
    $res = $dbh->query("SELECT * FROM phptest WHERE a = 72 ORDER BY b");
216
    $i = 0;
217
    while ($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) {
218
        print '|' . implode(" - ", $row) . "|\n";
219
        $i++;
220
    }
221
    if (!$i) {
222
        print "The records were not found.  Did they get inserted?\n";
223
    }
224
}
225
 
226
/**
227
 * Execute the prepared statement and print out the data in the result
228
 *
229
 * @param resource     $sth   the statement handle to process
230
 * @param string|array $bind  the data that will replace the placeholders
231
 *
232
 * @return void
233
 */
234
function print_4($sth, $bind) {
235
    global $dbh;
236
    $res = $dbh->execute($sth, $bind);
237
    while ($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) {
238
        print '|' . implode(" - ", $row) . "|\n";
239
    }
240
    echo "~~\n";
241
}