home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / errors.inc < prev    next >
Encoding:
Text File  |  2004-03-24  |  4.1 KB  |  134 lines

  1. <?php
  2.  
  3. function errhandler($obj)
  4. {
  5.     if ($obj->getCode() == DB_ERROR) {
  6.         print '  unknown: ' . $obj->getUserInfo()."\n";
  7.     } else {
  8.         $msg = $obj->getMessage();
  9.         print "  $msg";
  10.         if (substr($msg, -1) != "\n") {
  11.             print "\n";
  12.         }
  13.     }
  14. }
  15.  
  16. $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, "errhandler");
  17.  
  18.  
  19. print "Trying to provoke DB_ERROR_NOSUCHTABLE\n";
  20. $dbh->query('SELECT * FROM tableThatsBogus');
  21.  
  22. print "Trying to provoke DB_ERROR_ALREADY_EXISTS for create table\n";
  23. $dbh->query($test_mktable_query);
  24.  
  25. print "Trying to provoke DB_ERROR_NOSUCHTABLE\n";
  26. $dbh->query('DROP TABLE tableThatsBogus');
  27.  
  28.  
  29. print "Trying to provoke DB_ERROR_CONSTRAINT for primary key insert duplicate\n";
  30. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  31. $dbh->query('DROP TABLE a');
  32. $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'errhandler');
  33. $dbh->query('CREATE TABLE a (a INTEGER NOT NULL, PRIMARY KEY (a))');
  34. $dbh->query('INSERT INTO a VALUES (1)');
  35. $dbh->query('INSERT INTO a VALUES (1)');
  36.  
  37. print "Trying to provoke DB_ERROR_CONSTRAINT for primary key update duplicate\n";
  38. $dbh->query('INSERT INTO a VALUES (2)');
  39. $dbh->query('UPDATE a SET a=1 WHERE a=2');
  40.  
  41.  
  42. print "Trying to provoke DB_ERROR_CONSTRAINT for unique key insert duplicate\n";
  43. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  44. $dbh->query('DROP TABLE a');
  45. $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'errhandler');
  46. $dbh->query('CREATE TABLE a (a INTEGER NOT NULL, UNIQUE (a))');
  47. $dbh->query('INSERT INTO a VALUES (1)');
  48. $dbh->query('INSERT INTO a VALUES (1)');
  49.  
  50. print "Trying to provoke DB_ERROR_CONSTRAINT for unique key update duplicate\n";
  51. $dbh->query('INSERT INTO a VALUES (2)');
  52. $dbh->query('UPDATE a SET a=1 WHERE a=2');
  53.  
  54.  
  55. print "Trying to provoke DB_ERROR_CONSTRAINT for foreign key\n";
  56. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  57. $dbh->query('DROP TABLE b');
  58. $dbh->query('DROP TABLE a');
  59. $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'errhandler');
  60. $dbh->query('CREATE TABLE a (a INTEGER NOT NULL, PRIMARY KEY(a))');
  61. $dbh->query('CREATE TABLE b (b INTEGER REFERENCES a(a))');
  62. $dbh->query('INSERT INTO a values (1)');
  63. $dbh->query('INSERT INTO b values (2)');
  64. switch ($dbh->phptype) {
  65.     case 'mysql':
  66.     case 'sqlite':
  67.         print "            OK  $dbh->phptype doesn't consider this an error\n";
  68.         break;
  69. }
  70.  
  71.  
  72. print "Trying to provoke DB_ERROR_CONSTRAINT_NOT_NULL on insert\n";
  73. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  74. $dbh->query('DROP TABLE peartestnull');
  75. $dbh->setErrorHandling(PEAR_ERROR_CALLBACK, 'errhandler');
  76. $dbh->query('CREATE TABLE peartestnull (a CHAR(3) NOT NULL)');
  77. $dbh->query('INSERT INTO peartestnull VALUES (NULL)');
  78.  
  79. print "Trying to provoke DB_ERROR_CONSTRAINT_NOT_NULL on update\n";
  80. $dbh->query("INSERT INTO peartestnull VALUES ('one')");
  81. $dbh->query("UPDATE peartestnull SET a = NULL WHERE a = 'one'");
  82. switch ($dbh->phptype) {
  83.     case 'mysql':
  84.         print "            OK  $dbh->phptype doesn't consider this an error\n";
  85.         break;
  86. }
  87.  
  88.  
  89. print "Trying to provoke DB_ERROR_DIVZERO\n";
  90. // Interbase detects the error on fetching
  91. $dbh->getAll('SELECT 0/0 FROM phptest');
  92. switch ($dbh->phptype) {
  93.     case 'mssql':
  94.     case 'sybase':
  95.         print "            OK  PHP's $dbh->phptype extension doesn't report this error\n";
  96.         break;
  97.     case 'odbc':
  98.         switch ($dbh->dbsyntax) {
  99.             case 'access':
  100.                 print "            OK  odbc($dbh->dbsyntax) doesn't report this error\n";
  101.                 break;
  102.         }
  103.         break;
  104.     case 'ifx':
  105.     case 'mysql':
  106.     case 'sqlite':
  107.         print "            OK  $dbh->phptype doesn't consider this an error\n";
  108.         break;
  109. }
  110.  
  111. print "Trying to provoke DB_ERROR_INVALID_NUMBER\n";
  112. $dbh->query("UPDATE phptest SET a = 'abc' WHERE a = 42");
  113. switch ($dbh->phptype) {
  114.     case 'mysql':
  115.     case 'sqlite':
  116.         print "            OK  $dbh->phptype doesn't consider this an error\n";
  117.         break;
  118. }
  119.  
  120. print "Trying to provoke DB_ERROR_NOSUCHFIELD\n";
  121. $dbh->query('SELECT e FROM phptest');
  122.  
  123. print "Trying to provoke DB_ERROR_SYNTAX\n";
  124. $dbh->query('CREATE');
  125.  
  126.  
  127. $dbh->setErrorHandling(PEAR_ERROR_RETURN);
  128. $dbh->query('DROP TABLE phptest');
  129. $dbh->query('DROP TABLE b');
  130. $dbh->query('DROP TABLE a');
  131. $dbh->query('DROP TABLE peartestnull');
  132.  
  133. ?>
  134.