DB
[ class tree: DB ] [ index: DB ] [ all elements ]

Source for file prepexe.inc

Documentation is available at prepexe.inc

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

Documentation generated on Tue, 20 Mar 2007 05:30:58 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.