home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / 18get.phpt < prev    next >
Encoding:
Text File  |  2004-03-24  |  14.3 KB  |  631 lines

  1. --TEST--
  2. DB_driver::get
  3. --SKIPIF--
  4. <?php
  5.  
  6. /**
  7.  * Calls the get*() methods in various ways against any DBMS.
  8.  *
  9.  * @see DB_Common::getAll(), DB_Common::getAssoc(), DB_Common::getCol()
  10.  *      DB_Common::getListOf(), DB_Common::getOne(), DB_Common::getRow()
  11.  *
  12.  * @package  DB
  13.  * @version  $Id: 18get.phpt,v 1.3 2004/02/18 05:38:11 danielc Exp $
  14.  * @category Database
  15.  * @author   Daniel Convissor <danielc@analysisandsolutions.com>
  16.  * @internal
  17.  */
  18.  
  19. chdir(dirname(__FILE__));
  20. require_once './driver/skipif.inc';
  21.  
  22. ?>
  23. --FILE--
  24. <?php
  25.  
  26. // $Id: 18get.phpt,v 1.3 2004/02/18 05:38:11 danielc Exp $
  27.  
  28. /**
  29.  * Connect to the database and make the <kbd>phptest</kbd> table.
  30.  */
  31. require_once './mktable.inc';
  32.  
  33.  
  34. /**
  35.  * Local error callback handler.
  36.  *
  37.  * Drops the phptest table, prints out an error message and kills the
  38.  * process.
  39.  *
  40.  * @param object  $o  PEAR error object automatically passed to this method
  41.  * @return void
  42.  * @see PEAR::setErrorHandling()
  43.  */
  44. function pe($o){
  45.     global $dbh;
  46.  
  47.     $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  48.     $dbh->query('DROP TABLE phptest');
  49.  
  50.     die($o->toString());
  51. }
  52.  
  53. $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'pe');
  54.  
  55.  
  56. $dbh->query("INSERT INTO phptest VALUES (2, 'two', 'Two', '2002-02-22')");
  57. $dbh->query("INSERT INTO phptest VALUES (42, 'three', 'Three', '2003-03-23')");
  58.  
  59.  
  60. print "===================================================\n";
  61. print 'testing getOne: ';
  62. $ret =& $dbh->getOne("SELECT * FROM phptest WHERE c = 'Two'");
  63. print_r($ret);
  64. print "\n";
  65.  
  66. print 'testing getOne with string params: ';
  67. $ret =& $dbh->getOne('SELECT * FROM phptest WHERE c = ?', 'Three');
  68. print_r($ret);
  69. print "\n";
  70.  
  71. print 'testing getOne with array params: ';
  72. $ret =& $dbh->getOne('SELECT * FROM phptest WHERE c = ?', array('Two'));
  73. print_r($ret);
  74. print "\n";
  75.  
  76. print "\n===================================================\n";
  77. print "testing getRow:\n";
  78. $ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'");
  79. print_r($ret);
  80.  
  81. print "testing getRow with null params, DB_FETCHMODE_ORDERED:\n";
  82. $ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'",
  83.         null, DB_FETCHMODE_ORDERED);
  84. print_r($ret);
  85.  
  86. // THIS DOESN'T WORK DUE TO BACKWARDS COMPATIBILITY CRAP
  87. // print "testing getRow with string params, DB_FETCHMODE_ORDERED:\n";
  88. // $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
  89. //         'Two', DB_FETCHMODE_ORDERED);
  90. // print_r($ret);
  91. //
  92. // testing getRow with string params, DB_FETCHMODE_ORDERED:
  93. // Array
  94. // (
  95. //     [0] => 2
  96. //     [1] => two
  97. //     [2] => Two
  98. //     [3] => 2002-02-22
  99. // )
  100.  
  101.    print "testing getRow with REVERSED args: DB_FETCHMODE_ASSOC, array params:\n";
  102.    $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
  103.            DB_FETCHMODE_ASSOC, array('Two'));
  104.    print_r($ret);
  105.    
  106.    print "testing getRow with REVERSED args: DB_FETCHMODE_ASSOC:\n";
  107.    $ret =& $dbh->getRow("SELECT * FROM phptest WHERE c = 'Two'",
  108.            DB_FETCHMODE_ASSOC);
  109.    print_r($ret);
  110.  
  111. print "testing getRow with array params, DB_FETCHMODE_ASSOC:\n";
  112. $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
  113.         array('Two'), DB_FETCHMODE_ASSOC);
  114. print_r($ret);
  115.  
  116. print "testing getRow with array params, DB_FETCHMODE_OBJECT:\n";
  117. $ret =& $dbh->getRow('SELECT * FROM phptest WHERE c = ?',
  118.         array('Two'), DB_FETCHMODE_OBJECT);
  119. print_r($ret);
  120.  
  121.  
  122. print "\n===================================================\n";
  123. print "testing getCol:\n";
  124. $ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b");
  125. print_r($ret);
  126.  
  127. print "testing getCol with 1 col:\n";
  128. $ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b", 1);
  129. print_r($ret);
  130.  
  131. print "testing getCol with b col:\n";
  132. $ret =& $dbh->getCol("SELECT * FROM phptest ORDER BY b", 'b');
  133. print_r($ret);
  134.  
  135. print "testing getCol with b col, scalar params:\n";
  136. $ret =& $dbh->getCol("SELECT * FROM phptest WHERE a < ? ORDER BY b",
  137.         'b', 100);
  138. print_r($ret);
  139.  
  140. print "testing getCol with b col, array params:\n";
  141. $ret =& $dbh->getCol("SELECT * FROM phptest WHERE a < ? ORDER BY b",
  142.         'b', array(100));
  143. print_r($ret);
  144.  
  145.  
  146. print "\n===================================================\n";
  147. print "testing getAssoc:\n";
  148. $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < 100 ORDER BY b');
  149. print_r($ret);
  150.  
  151. print "testing getAssoc with false force, null params, DB_FETCHMODE_ORDERED:\n";
  152. $ret =& $dbh->getAssoc("SELECT a, b, c FROM phptest WHERE a < 100 ORDER BY b",
  153.                         false, null, DB_FETCHMODE_ORDERED);
  154. print_r($ret);
  155.  
  156. print "testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC:\n";
  157. $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
  158.                         false, 100, DB_FETCHMODE_ASSOC);
  159. print_r($ret);
  160.  
  161. print "testing getAssoc with two cols, false force, scalar params, DB_FETCHMODE_ASSOC:\n";
  162. $ret =& $dbh->getAssoc('SELECT a, b FROM phptest WHERE a < ? ORDER BY b',
  163.                         false, 100, DB_FETCHMODE_ASSOC);
  164. print_r($ret);
  165.  
  166. print "testing getAssoc with two cols, true force, scalar params, DB_FETCHMODE_ASSOC:\n";
  167. $ret =& $dbh->getAssoc('SELECT a, b FROM phptest WHERE a < ? ORDER BY b',
  168.                         true, 100, DB_FETCHMODE_ASSOC);
  169. print_r($ret);
  170.  
  171. print "testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC, true group:\n";
  172. $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
  173.                         false, 100, DB_FETCHMODE_ASSOC, true);
  174. print_r($ret);
  175.  
  176. print "testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:\n";
  177. $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
  178.                         false, array(100), DB_FETCHMODE_OBJECT);
  179. print_r($ret);
  180.  
  181. print "testing getAssoc with true force, array params, DB_FETCHMODE_OBJECT, true group:\n";
  182. $ret =& $dbh->getAssoc('SELECT a, b, c FROM phptest WHERE a < ? ORDER BY b',
  183.                         false, array(100), DB_FETCHMODE_OBJECT, true);
  184. print_r($ret);
  185.  
  186.  
  187. print "\n===================================================\n";
  188. print "testing getAll:\n";
  189. $ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'");
  190. print_r($ret);
  191.  
  192. print "testing getAll with null params, DB_FETCHMODE_ORDERED:\n";
  193. $ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'",
  194.         null, DB_FETCHMODE_ORDERED);
  195. print_r($ret);
  196.  
  197. // THIS DOESN'T WORK DUE TO BACKWARDS COMPATIBILITY CRAP
  198. // print "testing getAll with string params, DB_FETCHMODE_ORDERED:\n";
  199. // $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ?',
  200. //         'Two', DB_FETCHMODE_ORDERED);
  201. // print_r($ret);
  202. //
  203. // testing getAll with string params, DB_FETCHMODE_ORDERED:
  204. // Array
  205. // (
  206. //     [0] => 2
  207. //     [1] => two
  208. //     [2] => Two
  209. //     [3] => 2002-02-22
  210. // )
  211.  
  212.    print "testing getAll with REVERSED args: DB_FETCHMODE_ASSOC, array params:\n";
  213.    $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
  214.            DB_FETCHMODE_ASSOC, array('Two', 'Three'));
  215.    print_r($ret);
  216.    
  217.    print "testing getAll with REVERSED args: DB_FETCHMODE_ASSOC:\n";
  218.    $ret =& $dbh->getAll("SELECT * FROM phptest WHERE c = 'Two' OR c = 'Three'",
  219.            DB_FETCHMODE_ASSOC);
  220.    print_r($ret);
  221.  
  222. print "testing getAll with array params, DB_FETCHMODE_ASSOC:\n";
  223. $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
  224.         array('Two', 'Three'), DB_FETCHMODE_ASSOC);
  225. print_r($ret);
  226.  
  227. print "testing getAll with array params, DB_FETCHMODE_OBJECT:\n";
  228. $ret =& $dbh->getAll('SELECT * FROM phptest WHERE c = ? OR c = ? ORDER BY c',
  229.         array('Two', 'Three'), DB_FETCHMODE_OBJECT);
  230. print_r($ret);
  231.  
  232.  
  233. print "\n===================================================\n";
  234. print 'testing getOne with null value in column: ';
  235. $dbh->query("INSERT INTO phptest VALUES (9, 'nine', '', NULL)");
  236.  
  237. $ret =& $dbh->getOne('SELECT d FROM phptest WHERE a = 9');
  238. if ($ret === '') {
  239.     print "empty string\n";
  240. } else {
  241.     print 'type=' . gettype($ret) . ", value=$ret\n";
  242. }
  243.  
  244. print 'testing getOne with empty string in column: ';
  245. $ret =& $dbh->getOne('SELECT c FROM phptest WHERE a = 9');
  246. if ($ret === '') {
  247.     print "empty string\n";
  248. } else {
  249.     print 'type=' . gettype($ret) . ", value=$ret\n";
  250. }
  251.  
  252.  
  253. print "\n===================================================\n";
  254.  
  255.  
  256. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  257. $dbh->query('DROP TABLE phptest');
  258.  
  259. ?>
  260. --EXPECT--
  261. ===================================================
  262. testing getOne: 2
  263. testing getOne with string params: 42
  264. testing getOne with array params: 2
  265.  
  266. ===================================================
  267. testing getRow:
  268. Array
  269. (
  270.     [0] => 2
  271.     [1] => two
  272.     [2] => Two
  273.     [3] => 2002-02-22
  274. )
  275. testing getRow with null params, DB_FETCHMODE_ORDERED:
  276. Array
  277. (
  278.     [0] => 2
  279.     [1] => two
  280.     [2] => Two
  281.     [3] => 2002-02-22
  282. )
  283. testing getRow with REVERSED args: DB_FETCHMODE_ASSOC, array params:
  284. Array
  285. (
  286.     [a] => 2
  287.     [b] => two
  288.     [c] => Two
  289.     [d] => 2002-02-22
  290. )
  291. testing getRow with REVERSED args: DB_FETCHMODE_ASSOC:
  292. Array
  293. (
  294.     [a] => 2
  295.     [b] => two
  296.     [c] => Two
  297.     [d] => 2002-02-22
  298. )
  299. testing getRow with array params, DB_FETCHMODE_ASSOC:
  300. Array
  301. (
  302.     [a] => 2
  303.     [b] => two
  304.     [c] => Two
  305.     [d] => 2002-02-22
  306. )
  307. testing getRow with array params, DB_FETCHMODE_OBJECT:
  308. stdClass Object
  309. (
  310.     [a] => 2
  311.     [b] => two
  312.     [c] => Two
  313.     [d] => 2002-02-22
  314. )
  315.  
  316. ===================================================
  317. testing getCol:
  318. Array
  319. (
  320.     [0] => 42
  321.     [1] => 42
  322.     [2] => 2
  323. )
  324. testing getCol with 1 col:
  325. Array
  326. (
  327.     [0] => bing
  328.     [1] => three
  329.     [2] => two
  330. )
  331. testing getCol with b col:
  332. Array
  333. (
  334.     [0] => bing
  335.     [1] => three
  336.     [2] => two
  337. )
  338. testing getCol with b col, scalar params:
  339. Array
  340. (
  341.     [0] => bing
  342.     [1] => three
  343.     [2] => two
  344. )
  345. testing getCol with b col, array params:
  346. Array
  347. (
  348.     [0] => bing
  349.     [1] => three
  350.     [2] => two
  351. )
  352.  
  353. ===================================================
  354. testing getAssoc:
  355. Array
  356. (
  357.     [42] => Array
  358.         (
  359.             [0] => three
  360.             [1] => Three
  361.         )
  362.  
  363.     [2] => Array
  364.         (
  365.             [0] => two
  366.             [1] => Two
  367.         )
  368.  
  369. )
  370. testing getAssoc with false force, null params, DB_FETCHMODE_ORDERED:
  371. Array
  372. (
  373.     [42] => Array
  374.         (
  375.             [0] => three
  376.             [1] => Three
  377.         )
  378.  
  379.     [2] => Array
  380.         (
  381.             [0] => two
  382.             [1] => Two
  383.         )
  384.  
  385. )
  386. testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC:
  387. Array
  388. (
  389.     [42] => Array
  390.         (
  391.             [b] => three
  392.             [c] => Three
  393.         )
  394.  
  395.     [2] => Array
  396.         (
  397.             [b] => two
  398.             [c] => Two
  399.         )
  400.  
  401. )
  402. testing getAssoc with two cols, false force, scalar params, DB_FETCHMODE_ASSOC:
  403. Array
  404. (
  405.     [42] => three
  406.     [2] => two
  407. )
  408. testing getAssoc with two cols, true force, scalar params, DB_FETCHMODE_ASSOC:
  409. Array
  410. (
  411.     [42] => Array
  412.         (
  413.             [b] => three
  414.         )
  415.  
  416.     [2] => Array
  417.         (
  418.             [b] => two
  419.         )
  420.  
  421. )
  422. testing getAssoc with false force, scalar params, DB_FETCHMODE_ASSOC, true group:
  423. Array
  424. (
  425.     [42] => Array
  426.         (
  427.             [0] => Array
  428.                 (
  429.                     [b] => bing
  430.                     [c] => This is a test
  431.                 )
  432.  
  433.             [1] => Array
  434.                 (
  435.                     [b] => three
  436.                     [c] => Three
  437.                 )
  438.  
  439.         )
  440.  
  441.     [2] => Array
  442.         (
  443.             [0] => Array
  444.                 (
  445.                     [b] => two
  446.                     [c] => Two
  447.                 )
  448.  
  449.         )
  450.  
  451. )
  452. testing getAssoc with false force, array params, DB_FETCHMODE_OBJECT:
  453. Array
  454. (
  455.     [42] => stdClass Object
  456.         (
  457.             [a] => 42
  458.             [b] => three
  459.             [c] => Three
  460.         )
  461.  
  462.     [2] => stdClass Object
  463.         (
  464.             [a] => 2
  465.             [b] => two
  466.             [c] => Two
  467.         )
  468.  
  469. )
  470. testing getAssoc with true force, array params, DB_FETCHMODE_OBJECT, true group:
  471. Array
  472. (
  473.     [42] => Array
  474.         (
  475.             [0] => stdClass Object
  476.                 (
  477.                     [a] => 42
  478.                     [b] => bing
  479.                     [c] => This is a test
  480.                 )
  481.  
  482.             [1] => stdClass Object
  483.                 (
  484.                     [a] => 42
  485.                     [b] => three
  486.                     [c] => Three
  487.                 )
  488.  
  489.         )
  490.  
  491.     [2] => Array
  492.         (
  493.             [0] => stdClass Object
  494.                 (
  495.                     [a] => 2
  496.                     [b] => two
  497.                     [c] => Two
  498.                 )
  499.  
  500.         )
  501.  
  502. )
  503.  
  504. ===================================================
  505. testing getAll:
  506. Array
  507. (
  508.     [0] => Array
  509.         (
  510.             [0] => 2
  511.             [1] => two
  512.             [2] => Two
  513.             [3] => 2002-02-22
  514.         )
  515.  
  516.     [1] => Array
  517.         (
  518.             [0] => 42
  519.             [1] => three
  520.             [2] => Three
  521.             [3] => 2003-03-23
  522.         )
  523.  
  524. )
  525. testing getAll with null params, DB_FETCHMODE_ORDERED:
  526. Array
  527. (
  528.     [0] => Array
  529.         (
  530.             [0] => 2
  531.             [1] => two
  532.             [2] => Two
  533.             [3] => 2002-02-22
  534.         )
  535.  
  536.     [1] => Array
  537.         (
  538.             [0] => 42
  539.             [1] => three
  540.             [2] => Three
  541.             [3] => 2003-03-23
  542.         )
  543.  
  544. )
  545. testing getAll with REVERSED args: DB_FETCHMODE_ASSOC, array params:
  546. Array
  547. (
  548.     [0] => Array
  549.         (
  550.             [a] => 42
  551.             [b] => three
  552.             [c] => Three
  553.             [d] => 2003-03-23
  554.         )
  555.  
  556.     [1] => Array
  557.         (
  558.             [a] => 2
  559.             [b] => two
  560.             [c] => Two
  561.             [d] => 2002-02-22
  562.         )
  563.  
  564. )
  565. testing getAll with REVERSED args: DB_FETCHMODE_ASSOC:
  566. Array
  567. (
  568.     [0] => Array
  569.         (
  570.             [a] => 2
  571.             [b] => two
  572.             [c] => Two
  573.             [d] => 2002-02-22
  574.         )
  575.  
  576.     [1] => Array
  577.         (
  578.             [a] => 42
  579.             [b] => three
  580.             [c] => Three
  581.             [d] => 2003-03-23
  582.         )
  583.  
  584. )
  585. testing getAll with array params, DB_FETCHMODE_ASSOC:
  586. Array
  587. (
  588.     [0] => Array
  589.         (
  590.             [a] => 42
  591.             [b] => three
  592.             [c] => Three
  593.             [d] => 2003-03-23
  594.         )
  595.  
  596.     [1] => Array
  597.         (
  598.             [a] => 2
  599.             [b] => two
  600.             [c] => Two
  601.             [d] => 2002-02-22
  602.         )
  603.  
  604. )
  605. testing getAll with array params, DB_FETCHMODE_OBJECT:
  606. Array
  607. (
  608.     [0] => stdClass Object
  609.         (
  610.             [a] => 42
  611.             [b] => three
  612.             [c] => Three
  613.             [d] => 2003-03-23
  614.         )
  615.  
  616.     [1] => stdClass Object
  617.         (
  618.             [a] => 2
  619.             [b] => two
  620.             [c] => Two
  621.             [d] => 2002-02-22
  622.         )
  623.  
  624. )
  625.  
  626. ===================================================
  627. testing getOne with null value in column: empty string
  628. testing getOne with empty string in column: empty string
  629.  
  630. ===================================================
  631.