Source for file prepexe.inc
Documentation is available at prepexe.inc
* Tests the drivers' prepare and execute methods
* Executed by driver/06prepexec.phpt
* 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.
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version $Id: prepexe.inc,v 1.27 2007/01/08 00:19:48 aharvey Exp $
* @link http://pear.php.net/package/DB
$tmpfile = tempnam("/tmp", "phptmp");
$fp = fopen($tmpfile, "w");
$filedata = "opaque placeholder's test";
* Local error callback handler
* Prints out an error message and kills the process.
* @param object $o PEAR error object automatically passed to this method
* @see PEAR::setErrorHandling()
print "\n" . $o->toString ();
$dbh->setErrorHandling (PEAR_ERROR_CALLBACK , 'pe');
// 1) Multiple prepare/exec INSERT queries
echo "------------1------------\n";
$sth1 = $dbh->prepare ("INSERT INTO phptest (a, b) VALUES (?, 'a')");
$sth2 = $dbh->prepare ("INSERT INTO phptest (a,b) VALUES (!,?)");
$sth3 = $dbh->prepare ("INSERT INTO phptest (a,b,cc) VALUES (?,!,&)");
$sth4 = $dbh->prepare ("INSERT INTO phptest (a, b) VALUES (72, 'direct')");
print "sth1,sth2,sth3,sth4 created\n";
print 'sth1: ? as param, passing as array... ';
if (($res = $dbh->execute ($sth1, array (72 ))) === DB_OK) {
print 'sth2: ! and ? as params, passing as array... ';
if (($res = $dbh->execute ($sth2, array (72 , "that's right"))) === DB_OK) {
print 'sth3: ?, ! and & as params, passing as array... ';
$res = $dbh->execute ($sth3, array (72 , "'it\\'s good'", $tmpfile));
$res = $dbh->execute ($sth3, array (72 , "'it''s good'", $tmpfile));
print 'sth4: no params... ';
if (($res = $dbh->execute ($sth4)) === DB_OK) {
// 2) One prepared, multiple time executed
echo "\n------------2------------\n";
$dbh->query ('DELETE FROM phptest');
$sth = $dbh->prepare ('INSERT INTO phptest (a, b, cc, d) VALUES (?, ?, &, ?)');
0 => array (72 , 'set1', $tmpfile, '1234-56-78'),
1 => array (72 , 'set2', $tmpfile, null ),
2 => array (72 , 'set3', $tmpfile, null )
$res = $dbh->executeMultiple ($sth, $data);
// 3) freePrepared() test
echo "\n------------3------------\n";
if ($dbh->freePrepared ($sth)) {
if ($dbh->freePrepared (666 )) {
echo "\n------------4------------\n";
$sth1 = $dbh->prepare ("SELECT * FROM phptest WHERE a = ? ORDER BY b");
$sth2 = $dbh->prepare ("SELECT * FROM phptest WHERE d = ? ORDER BY b");
$sth3 = $dbh->prepare ("SELECT * FROM phptest WHERE cc = & ORDER BY b");
// 5) ASSOCIATIVE ARRAY queries
echo "\n------------5------------\n";
$sth5 = $dbh->prepare ('INSERT INTO phptest (a, b, d) VALUES (?, ?, ?)');
$res = $dbh->execute ($sth5, $array);
print 'insert: ' . ($res === DB_OK ? 'okay' : 'error') . "\n";
$sth6 = $dbh->prepare ('SELECT a, b, d FROM phptest WHERE a = ?');
$res = $dbh->execute ($sth6, array (11 ));
print " a = {$row['a']}, b = {$row['b']}, d = ";
if ($dbh->phptype == 'msql') {
if ($type == 'NULL' || $row['d'] == '') {
print "got expected outcome\n";
print " UN-expected outcome: $type\n";
// 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";
print "got expected outcome\n";
print " UN-expected outcome: $type\n";
* Automatically free the prepared statements and results when the script
global $tmpfile, $dbh, $sth1, $sth2, $sth3, $sth4, $sth5, $sth6, $res;
* Interbase doesn't allow dropping tables that have result
$dbh->freePrepared ($sth1);
$dbh->freePrepared ($sth2);
$dbh->freePrepared ($sth3);
$dbh->freePrepared ($sth4);
$dbh->freePrepared ($sth5);
$dbh->freePrepared ($sth6);
$dbh->freeResult ($res->result );
$dbh->setErrorHandling (PEAR_ERROR_RETURN );
drop_table ($dbh, 'phptest');
* Print out the data in test table
$res = $dbh->query ("SELECT * FROM phptest WHERE a = 72 ORDER BY b");
print '|' . implode(" - ", $row) . "|\n";
print "The records were not found. Did they get inserted?\n";
* Execute the prepared statement and print out the data in the result
* @param resource $sth the statement handle to process
* @param string|array$bind the data that will replace the placeholders
$res = $dbh->execute ($sth, $bind);
print '|' . implode(" - ", $row) . "|\n";
Documentation generated on Tue, 20 Mar 2007 05:30:58 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|