[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 3 $tmpfile = tempnam("/tmp", "phptmp"); 4 register_shutdown_function("my_shutdown"); 5 $fp = fopen($tmpfile, "w"); 6 $filedata = "opaque placeholder's test"; 7 fwrite($fp, $filedata); 8 fclose($fp); 9 10 11 /** 12 * Local error callback handler. 13 * 14 * Prints out an error message and kills the process. 15 * 16 * @param object $o PEAR error object automatically passed to this method 17 * @return void 18 * @see PEAR::setErrorHandling() 19 */ 20 function pe($o) { 21 print "\n" . $o->toString(); 22 exit; 23 } 24 25 $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe'); 26 27 28 // 1) Multiple prepare/exec INSERT queries 29 echo "------------1------------\n"; 30 31 $sth1 = $dbh->prepare("INSERT INTO phptest (a, b) VALUES (?, 'a')"); 32 $sth2 = $dbh->prepare("INSERT INTO phptest (a,b) VALUES (!,?)"); 33 $sth3 = $dbh->prepare("INSERT INTO phptest (a,b,c) VALUES (?,!,&)"); 34 $sth4 = $dbh->prepare("INSERT INTO phptest (a, b) VALUES (72, 'direct')"); 35 print "sth1,sth2,sth3,sth4 created\n"; 36 print 'sth1: ? as param, passing as array... '; 37 if (($res = $dbh->execute($sth1, array(72))) === DB_OK) { 38 print "sth1 executed\n"; 39 } else { 40 print "sth1 failed\n"; 41 } 42 print 'sth2: ! and ? as params, passing as array... '; 43 if (($res = $dbh->execute($sth2, array(72, "that's right"))) === DB_OK) { 44 print "sth2 executed\n"; 45 } else { 46 print "sth2 failed\n"; 47 } 48 print 'sth3: ?, ! and & as params, passing as array... '; 49 if (($res = $dbh->execute($sth3, array(72, "'it''s good'", $tmpfile))) === DB_OK) { 50 print "sth3 executed\n"; 51 } else { 52 print "sth3 failed\n"; 53 } 54 print 'sth4: no params... '; 55 if (($res = $dbh->execute($sth4)) === DB_OK) { 56 print "sth4 executed\n"; 57 } else { 58 print "sth4 failed\n"; 59 } 60 print_results(); 61 62 63 // 2) One prepared, multiple time executed 64 echo "\n------------2------------\n"; 65 66 $dbh->query('DELETE FROM phptest'); 67 $sth = $dbh->prepare('INSERT INTO phptest (a, b, c, d) VALUES (?, ?, &, ?)'); 68 $data = array( 69 0 => array(72, 'set1', $tmpfile, '1234-56-78'), 70 1 => array(72, 'set2', $tmpfile, null), 71 2 => array(72, 'set3', $tmpfile, null) 72 ); 73 $res = $dbh->executeMultiple($sth, $data); 74 print_results(); 75 76 77 // 3) freePrepared() test 78 echo "\n------------3------------\n"; 79 80 if ($dbh->freePrepared($sth)) { 81 echo 'TRUE'; 82 } else { 83 echo 'FALSE'; 84 } 85 echo "\n"; 86 if ($dbh->freePrepared(666)) { 87 echo 'TRUE'; 88 } else { 89 echo 'FALSE'; 90 } 91 echo "\n"; 92 93 94 // 4) SELECTs tests 95 echo "\n------------4------------\n"; 96 97 $sth1 = $dbh->prepare("SELECT * FROM phptest WHERE a = ? ORDER BY b"); 98 print_4($sth1, 72); 99 print_4($sth1, 71); 100 $sth2 = $dbh->prepare("SELECT * FROM phptest WHERE d = ? ORDER BY b"); 101 print_4($sth2, '1234-56-78'); 102 $sth3 = $dbh->prepare("SELECT * FROM phptest WHERE c = & ORDER BY b"); 103 print_4($sth3, $tmpfile); 104 105 106 // 5) ASSOCIATIVE ARRAY queries 107 echo "\n------------5------------\n"; 108 109 $sth5 = $dbh->prepare('INSERT INTO phptest (a, b, d) VALUES (?, ?, ?)'); 110 $array = array( 111 'foo' => 11, 112 'bar' => 'three', 113 'baz' => null, 114 ); 115 $res = $dbh->execute($sth5, $array); 116 print 'insert: ' . ($res === DB_OK ? 'okay' : 'error') . "\n"; 117 118 $sth6 = $dbh->prepare('SELECT a, b, d FROM phptest WHERE a = ?'); 119 $res = $dbh->execute($sth6, array(11)); 120 $row = $res->fetchRow(DB_FETCHMODE_ASSOC); 121 print "a = {$row['a']}, b = {$row['b']}, d = " . gettype($row['d']) . "\n"; 122 123 124 125 function my_shutdown() { 126 global $tmpfile, $dbh, $sth1, $sth2, $sth3, $sth4, $sth5, $sth6, $res; 127 128 switch ($dbh->phptype) { 129 case 'ibase': 130 /* 131 * Interbase doesn't allow dropping tables that have result 132 * sets still open. 133 */ 134 $dbh->freePrepared($sth1); 135 $dbh->freePrepared($sth2); 136 $dbh->freePrepared($sth3); 137 $dbh->freePrepared($sth4); 138 $dbh->freePrepared($sth5); 139 $dbh->freePrepared($sth6); 140 $dbh->freeResult($res->result); 141 break; 142 } 143 144 $dbh->setErrorHandling(PEAR_ERROR_RETURN); 145 $dbh->query('DROP TABLE phptest'); 146 147 unlink($tmpfile); 148 } 149 150 function print_results() { 151 global $dbh; 152 print "results:\n"; 153 $res = $dbh->query("SELECT * FROM phptest WHERE a = 72 ORDER BY b"); 154 $i = 0; 155 while ($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) { 156 print '|' . implode(" - ", $row) . "|\n"; 157 $i++; 158 } 159 if (!$i) { 160 print "The records were not found. Did they get inserted?\n"; 161 } 162 } 163 164 function print_4($sth, $bind) { 165 global $dbh; 166 $res = $dbh->execute($sth, $bind); 167 while ($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) { 168 print '|' . implode(" - ", $row) . "|\n"; 169 } 170 echo "~~\n"; 171 } 172 173 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |