home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / diow.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  7.5 KB  |  287 lines

  1. database independence object wrapper 
  2.  
  3. This is a database abstraction layer, so that code can be written to run with any database. If you wish to switch database types, just change a parameter in the abstraction class ... not your entire code!! Currently supports mysql and postgres, I hope to add oracle and others. If you have any comments/suggestions, pls email me at manik@post1.com. 
  4.  
  5. <?php 
  6.  
  7. //------------------------------------------------------- 
  8. //     DATABASE.INDEPENDENCE.OBJECT.WRAPPER 
  9. //     VERSION 0.30 
  10. //     VERSION DATE: 15th July 1999 
  11. //------------------------------------------------------- 
  12. //     Author: Manik Surtani <manik@post1.com> 
  13. //------------------------------------------------------- 
  14.  
  15.  
  16.  
  17. //------------------------------------------------------- 
  18. // This version currently supports POSTGRESQL and MYSQL 
  19. // databases.  More support to be added sometime .... !! 
  20. //------------------------------------------------------- 
  21. // This wrapper provides 'physical' database connectivity 
  22. // and works as the link between the php3-based web site  
  23. // and the selected database.  
  24. //------------------------------------------------------- 
  25.  
  26.  
  27. // set the database TYPE.   
  28. //$database_type==1 --> POSTGRES 
  29. //$database_type==2 --> MYSQL 
  30.  
  31.  
  32. $database_type=2; 
  33.  
  34. /* 
  35.  
  36. --------------------------------------------------------- 
  37. "class resultSet" deals with a recordset produced by a 
  38. databse query. 
  39. --------------------------------------------------------- 
  40.  
  41. */ 
  42.  
  43.  
  44. class resultSet { 
  45.   var $element = array(); 
  46.   var $column_name = array(); 
  47.   var $numrows; 
  48.   var $numcols; 
  49.  
  50. function getColumn_by_name($rowNum,$colName) { 
  51.   $j=-1; 
  52.   for($i=0;$i<$this->numcols;$i++) 
  53.   { 
  54.    if ($this->column_name[$i]==$colName) { $j=$i; } 
  55.   } 
  56.   if ($j!=-1) { return $this->element[$rowNum][$j];} else { return  ""; } 
  57.  
  58. function getColumn_by_num($rowNum,$colNum) { 
  59.   return $this->element[$rowNum][$colNum]; 
  60.  
  61. function getColName($column) { 
  62.    return $this->column_name[$column]; 
  63.  
  64. function getNumRows()  { 
  65.    return $this->numrows; 
  66.  
  67. function getNumCols()  { 
  68.    return $this->numcols; 
  69.  
  70.  
  71.  
  72.  
  73. /* 
  74.  
  75. --------------------------------------------------------- 
  76. "class ConnInfo" provides a connection details object which returns information 
  77. given on a connection parameter to a database. 
  78. --------------------------------------------------------- 
  79.  
  80. */ 
  81.  
  82. class ConnectionInfo { 
  83.   var $cdbname; 
  84.   var $cusername; 
  85.   var $cpassword; 
  86.   var $chost; 
  87.   var $cport; 
  88.  
  89. function SplitThis($cparam) { 
  90.     $tok = strtok($cparam, " "); 
  91.     while($tok) { 
  92.         $pos=strpos($tok, "="); 
  93.         $type=substr($tok,0,$pos); 
  94.         switch ($type) { 
  95.            case  "dbname": 
  96.               $this->cdbname=substr($tok,$pos+1); 
  97.               break; 
  98.            case  "host": 
  99.               $this->chost=substr($tok,$pos+1); 
  100.               break; 
  101.            case  "port": 
  102.               $this->cport=substr($tok,$pos+1); 
  103.               break; 
  104.            case  "username": 
  105.               $this->cusername=substr($tok,$pos+1); 
  106.               break; 
  107.            case  "password": 
  108.               $this->cpassword=substr($tok,$pos+1); 
  109.               break; 
  110.         } 
  111.         $tok = strtok( " "); 
  112.     }   
  113.  
  114. function dbName() { return $this->cdbname; } 
  115. function Host() { return $this->chost; } 
  116. function Port() { return $this->cport; } 
  117. function UserName() { return $this->cusername; } 
  118. function Password() { return $this->cpassword; } 
  119.   
  120.  
  121. /* 
  122.  
  123. --------------------------------------------------------- 
  124. "class connection" provides functionality to deal with the 
  125. actual database. 
  126. --------------------------------------------------------- 
  127.  
  128. */ 
  129.  
  130.  
  131. class connection { 
  132.   var $my_connection; 
  133.   var $my_temp_resultID; 
  134.   var $my_temp_result_object = new resultSet; 
  135.  
  136.   function open($p1, $p2 =  "", $p3 =  "", $p4 =  "", $p5= "" )  { 
  137.   global $database_type; 
  138.   $ok = false; 
  139.  
  140.  
  141.   if (($p2 ==  "") && ($p3 ==  "") && ($p4 ==  "") && ($p5 ==  "") && ($database_type==1)) 
  142.   { 
  143.     $this->my_connection=pg_connect($p1); 
  144.     $ok = true; 
  145.   } 
  146.  
  147.   if (($p5 ==  "") && ($database_type==2)) 
  148.   { 
  149.      $connINF = new ConnectionInfo; 
  150.      $connINF->SplitThis($p1); 
  151.      $p1=$connINF->Host(); 
  152.      $p2=$connINF->UserName(); 
  153.      $p3=$connINF->Password(); 
  154.      $p4=$connINF->dbName(); 
  155.     $this->my_connection=mysql_connect($p1, $p2, $p3); 
  156.     mysql_select_db($p4,$this->my_connection); 
  157.     $ok = true; 
  158.   } 
  159.  
  160.   if (!($ok)) 
  161.   { 
  162.     print  "\n\nTHERE WAS AN ERROR CONNECTING.\n\n"; 
  163.   } 
  164.   }     
  165.  
  166. function close() { 
  167.   global $database_type; 
  168.  
  169. if ($database_type==1) 
  170.     { 
  171.     pg_close($this->my_connection); 
  172.     } 
  173.  
  174. if ($database_type==2) 
  175.     { 
  176.     mysql_close($this->my_connection); 
  177.     } 
  178.  
  179.  
  180.  
  181. function runActionQuery($someSQL) 
  182.  
  183. global $database_type; 
  184.  
  185.     // cleanup SQL for PHP versions! (sic) 
  186.    if (substr($someSQL,strlen($someSQL)-1,1)== ";") { 
  187.       $someSQL=substr($someSQL,0,strlen($someSQL)-1); 
  188.     } 
  189.  
  190.     if ($database_type==1) 
  191.     { 
  192.     pg_exec($this->my_connection, $someSQL); 
  193.     } 
  194.      
  195.     if ($database_type==2) 
  196.     { 
  197.     mysql_query($someSQL, $this->my_connection); 
  198.     } 
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. function runSQL($someSQL)     
  207.     global $database_type; 
  208.  
  209.     // cleanup SQL for PHP versions! (sic) 
  210.    if (substr($someSQL,strlen($someSQL)-1,1)== ";") { 
  211.       $someSQL=substr($someSQL,0,strlen($someSQL)-1); 
  212.     } 
  213.  
  214.     if ($database_type==1) 
  215.     { 
  216.      
  217.         $this->my_temp_resultID = pg_exec($this->my_connection, $someSQL); 
  218.         $this->my_temp_result_object->numrows = pg_numrows($this->my_temp_resultID); 
  219.         $this->my_temp_result_object->numcols = pg_numfields($this->my_temp_resultID); 
  220.  
  221.            // fill column_names from resultset 
  222.         for ($j=0; $j < $this->my_temp_result_object->numcols; $j++) 
  223.         { 
  224.             $this->my_temp_result_object->column_name[$j] = pg_fieldname($this->my_temp_resultID, $j); 
  225.         } 
  226.  
  227.            // fill data elements from resultset 
  228.         for ($i=0; $i < $this->my_temp_result_object->numrows; $i++) 
  229.         { 
  230.             for ($j=0; $j < $this->my_temp_result_object->numcols; $j++) 
  231.             { 
  232.                $this->my_temp_result_object->element[$i][$j] = pg_result($this->my_temp_resultID, $i, $j); 
  233.             } 
  234.         } 
  235.  
  236.         return $this->my_temp_result_object; 
  237.           pg_freeresult($this->my_temp_resultID); 
  238.     } 
  239.  
  240.  
  241. if ($database_type==2) 
  242.     { 
  243.     $this->my_temp_resultID = mysql_query($someSQL, $this->my_connection); 
  244.     $this->my_temp_result_object->numrows = mysql_num_rows($this->my_temp_resultID); 
  245.     $this->my_temp_result_object->numcols = mysql_num_fields($this->my_temp_resultID); 
  246.          
  247.        // fill column_names from resultset 
  248.     for ($j=0; $j < $this->my_temp_result_object->numcols; $j++) 
  249.     { 
  250.         $this->my_temp_result_object->column_name[$j] = mysql_fieldname($this->my_temp_resultID, $j); 
  251.     } 
  252.  
  253.        // fill data elements from resultset 
  254.     for ($i=0; $i < $this->my_temp_result_object->numrows; $i++) 
  255.         { 
  256.         $x = mysql_fetch_row($this->my_temp_resultID); 
  257.         for ($j=0; $j < $this->my_temp_result_object->numcols; $j++) 
  258.             { 
  259.             $this->my_temp_result_object->element[$i][$j] = $x[$j]; 
  260.             } 
  261.         } 
  262.  
  263.     return $this->my_temp_result_object; 
  264.     mysql_free_result($this->my_temp_resultID); 
  265.     } 
  266.  
  267.      
  268.     } 
  269.  
  270.  
  271. }  
  272.  
  273.  
  274. ?> 
  275.