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

  1. simple database wrap 
  2.  
  3. This is a database wrapper for PostgreSQL, but can be simply modified for any other database type. 
  4.  
  5. <?php 
  6.  
  7. if($dbObjDefined != 1) 
  8.  
  9.   $dbObjDefined = 1; 
  10.  
  11.    //Wrapper class for database calls 
  12.   class dbObj 
  13.   { 
  14.      //Connection handle to database 
  15.     var $conn; 
  16.      
  17.      //Default connection parameters 
  18.     var $host =  "YourSite.com"; 
  19.     var $user =  "johndoe"; 
  20.     var $password =  "pwd"; 
  21.     var $port =  "5432"; 
  22.     var $dbname =  "MyDB"; 
  23.  
  24.      //Open initial connection. $params is an associative array holding  
  25.      //parameters to the pg_Connect function. 
  26.     function init($params) 
  27.     { 
  28.  
  29.       if(isset($parame[host])) 
  30.         $host = $parame[host]; 
  31.       else 
  32.         $host = $this->host; 
  33.  
  34.       if(isset($parame[user])) 
  35.         $user = $parame[user]; 
  36.       else 
  37.         $user = $this->user; 
  38.  
  39.       if(isset($parame[password])) 
  40.         $password = $parame[password]; 
  41.       else 
  42.         $password = $this->password; 
  43.  
  44.       if(isset($parame[port])) 
  45.         $port = $parame[port]; 
  46.       else 
  47.         $port = $this->port; 
  48.  
  49.       if(isset($parame[dbname])) 
  50.         $dbname = $parame[dbname]; 
  51.       else 
  52.         $dbname = $this->dbname; 
  53.  
  54.       $this->conn = pg_Connect ( " host=$host user=$user password=$password port=$port dbname=$dbname ");   
  55.     } 
  56.  
  57.      //Send SQL to database connection.   
  58.      //Return recordset object on success. 
  59.      //Return 0 on failure. 
  60.     function exec($SQL) 
  61.     { 
  62.       $resultset = pg_Exec($this->conn, $SQL); 
  63.  
  64.       if ($resultset) { 
  65.         $recset = new recordset; 
  66.  
  67.         $recset->init($resultset); 
  68.  
  69.         return $recset; 
  70.       } 
  71.       else { 
  72.         return 0; 
  73.       } 
  74.     }     
  75.  
  76.      //Close connection to database 
  77.     function free() 
  78.     { 
  79.       pg_close($this->conn); 
  80.     } 
  81.   }; 
  82.  
  83.    /* 
  84.     This is a simple recordset class which can be  
  85.     traversed using next(), prev(), and current() methods. 
  86.     It is initialized from a resultset returned from the  
  87.     function "pg_Exec" or can be generated by a call to the 
  88.     exec method from the dbObj. 
  89.   */ 
  90.   class recordset 
  91.   { 
  92.     var $resultset; 
  93.     var $index; 
  94.     var $numFields; 
  95.     var $numTuples; 
  96.  
  97.     function init($newResultset) 
  98.     { 
  99.       $this->resultset = $newResultset; 
  100.       $this->index = 0; 
  101.       $this->numFields = pg_NumFields($this->resultset); 
  102.       $this->numTuples = pg_NumRows($this->resultset); 
  103.     } 
  104.  
  105.      //Get a value by row name and either column name or column number 
  106.     function getVal($row, $col) 
  107.     { 
  108.       return pg_Result($this->resultset, $row, $col); 
  109.     } 
  110.  
  111.      //Return an array of field names 
  112.     function getFields() 
  113.     { 
  114.       for($i=0; $i<$this->numFields; $i++) 
  115.         $retArray[] = pg_FieldName($this->resultset, $i); 
  116.  
  117.       return $retArray; 
  118.     } 
  119.  
  120.      //Get number of columns in resultset 
  121.     function getNumFields() 
  122.     { 
  123.       return $this->numFields; 
  124.     } 
  125.  
  126.      //Get a tuple (associative array of column values) by row number 
  127.     function getTupleDirect($row) 
  128.     { 
  129.       for($i=0; $i<$this->numFields; $i++) 
  130.         $retArray[pg_FieldName($this->resultset, $i)] =  
  131.           pg_Result($this->resultset, $row, $i); 
  132.  
  133.       return $retArray; 
  134.     } 
  135.  
  136.      //Get tuple pointed to by the current index 
  137.     function getTuple() 
  138.     { 
  139.       if($this->index>=0 && $this->index < $this->numTuples) 
  140.         return $this->getTupleDirect($this->index); 
  141.       else 
  142.         return 0; 
  143.     } 
  144.  
  145.      //Get an array filled with all values in a column 
  146.      //(using either column name or column number) 
  147.     function getColumn($col) 
  148.     { 
  149.       for($i=0; $i<$this->numTuples; $i++) 
  150.         $retArray[] = pg_Result($this->resultset, $i, $col); 
  151.  
  152.       return $retArray; 
  153.     } 
  154.  
  155.      //Return the number of records in the recordset 
  156.     function getNumTuples() 
  157.     { 
  158.       return $this->numTuples; 
  159.     } 
  160.  
  161.      //Return 1 if index is within bounds of the recordset 
  162.     function current() 
  163.     { 
  164.       if($this->index>=0 && $this->index < $this->numTuples) 
  165.         return 1; 
  166.       else 
  167.         return 0; 
  168.     } 
  169.  
  170.      //Incriment index 
  171.     function next() 
  172.     { 
  173.       if($this->index<$this->numTuples) 
  174.       { 
  175.         $this->index++; 
  176.         return 1; 
  177.       } 
  178.       else 
  179.       { 
  180.         return 0; 
  181.       } 
  182.     } 
  183.  
  184.      //Decriment index 
  185.     function prev() 
  186.     { 
  187.       if($this->index >= 0) 
  188.       { 
  189.         $this->index--; 
  190.         return 1; 
  191.       } 
  192.       else 
  193.       { 
  194.         return 0; 
  195.       } 
  196.     } 
  197.  
  198.      //Reset index to 0 
  199.     function reset() 
  200.     { 
  201.       $this->index = 0; 
  202.     } 
  203.  
  204.      //Free memory allocated to recordset. 
  205.     function free() 
  206.     { 
  207.       pg_Freeresult($this->resultset); 
  208.     } 
  209.   }; 
  210.  
  211.  
  212. ?> 
  213.  
  214.