home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / php.exe / small.php < prev    next >
Encoding:
PHP Script  |  2001-07-02  |  4.6 KB  |  199 lines

  1.     //**************************************
  2.     //     
  3.     // Name: A Small Oracle Class
  4.     // Description:a small oracle class with
  5.     //     OCI easy to manipulate Oracle database b
  6.     //     y Likai
  7.     // By: PHP Code Exchange
  8.     //**************************************
  9.     //     
  10.     
  11.     <?PHP
  12.     /* 
  13.     Defination of ORACLE db CLASS and ORACLE query CLASS
  14.     Author: Likai
  15.     */
  16.     if ( !defined( "_ORACLE_CLASS" ) )
  17.     {
  18.     define("_ORACLE_CLASS", 1 );
  19.     /* 
  20.     ORACLE database class 
  21.     function open :open oraclass database connection 
  22.     function close: close the database connection and free the smtm
  23.     function addqury: add the smtm ready for free
  24.     */
  25.     class ora_db
  26.     {
  27.     var $connect_id; 
  28.     
  29.     //function to open ORACLE database
  30.     function open($database, $user, $password) 
  31.     {
  32.     $this->connect_id = OCILogon($user, $password, $database); 
  33.     if( $this->connect_id )
  34.     return $this->connect_id;
  35.     else
  36.     return FALSE;
  37.     }
  38.     
  39.     
  40.     // Closes the database connection and fr
  41.     //     ees any query results left.
  42.     function close()
  43.     {
  44.     if($this->stmt_id && is_array($this->stmt_id))
  45.     {
  46.     while (list($key,$val)=each($this->stmt_id))
  47.     {
  48.     @OCIFreeStatement($val) ;
  49.     }
  50.     }
  51.     $result = OCILogoff($this->connect_id); 
  52.     return$result; 
  53.     }
  54.     
  55.     // Function used by the constructor of q
  56.     //     uery.
  57.     function addstmt($stmt_id)
  58.     {
  59.     $this->stmt_id[]=$stmt_id;
  60.     }
  61.     }
  62.     
  63.     /* End of Defination of ORACLE db Class */
  64.     /* 
  65.     Defination of ORACLE query Class 
  66.     
  67.     */
  68.     class ora_query
  69.     {
  70.     var $row;
  71.     var $smtm;
  72.     var $bind_data;
  73.     
  74.     //new a query and directly execute the n
  75.     //     ormal query
  76.     function ora_query(&$db, $query="")
  77.     {
  78.     if($query!=""&&$db->connect_id)
  79.     {
  80.     $this->stmt = OCIParse($db->connect_id, $query); 
  81.     if (!$this->stmt)
  82.     { 
  83.     return false; 
  84.     } 
  85.     if (OCIExecute($this->stmt)) 
  86.     { 
  87.     $db->addstmt($this->stmt);
  88.     return $this->stmt; 
  89.     } 
  90.     OCIFreeStatement($this->stmt); 
  91.     return false; 
  92.     }
  93.     else
  94.     return FALSE;
  95.     }
  96.     /* 
  97.     before execute the query ,BIND the query with the php extern variables 
  98.     for example $query = "insert into ? values(?,'?',?,0)
  99.     then the function analye the string 
  100.     */
  101.     function prepare(&$db,$query="")
  102.     {
  103.     $pieces = explode( "?", $query); 
  104.     $new_query = ""; 
  105.     $last_piece = sizeof($pieces) - 1; 
  106.     while (list($i, $piece) = each($pieces)) 
  107.     { 
  108.     $new_query .= $piece; 
  109.     if ($i < $last_piece) 
  110.     { 
  111.     $new_query .= ":var$i"; 
  112.     } 
  113.     } 
  114.     print "<br>new_query=$new_query\n<br>";
  115.     //for debug
  116.     $this->stmt = OCIParse($db->connect_id, $new_query); 
  117.     if (!$this->stmt) 
  118.     { 
  119.     return false; 
  120.     } 
  121.     for ($i = 0; $i < $last_piece; $i++) 
  122.     { 
  123.     $bindvar = ":var$i"; 
  124.     if (!OCIBindByName($this->stmt, $bindvar, &$this->bind_data[$i],32))
  125.     { 
  126.         OCIFreeStatement($this->stmt); 
  127.     return FALSE;
  128.     } 
  129.     } 
  130.     return $this->stmt; 
  131.     }
  132.     //after prepare query then execute the q
  133.     //     uery
  134.     function execute(&$db,$data)
  135.     {
  136.     while (list($i, $value) = each($data)) 
  137.     { 
  138.     $this->bind_data[$i] = $data[$i]; 
  139.          echo $this->bind[$i];
  140.     } 
  141.     if (OCIExecute($this->stmt)) 
  142.     { 
  143.     $db->addstmt($this->stmt);
  144.     return $this->stmt;
  145.         }
  146.     return FALSE;
  147.     }
  148.     // fetch the next row of stmt
  149.     function getrow()
  150.     { 
  151.     if(!OCIFetchInto($this->stmt, &$this->row,OCI_ASSOC)) 
  152.     { 
  153.     return FALSE;
  154.     } 
  155.         var_dump($this->row);
  156.     return $this->row;
  157.     } 
  158.     // get the number of rows of $stmt
  159.     function numrows()
  160.     {
  161.     $number=OCIRowCount($this->stmt) ;
  162.     return $number;
  163.     }
  164.     //get oracle runtime error 
  165.     function error()
  166.     {
  167.     $error=OCIError($this->stmt);
  168.     if( $error )
  169.     return $errot;
  170.     else
  171.     return OCIError($db->connect_id);
  172.     }
  173.     
  174.     
  175.     }
  176.     
  177.     
  178.     /* End of Defination of ORACLE query Class */
  179.     }
  180.     /*End ifdefined */
  181.     //example:
  182.     $DB = new ora_class;
  183.     $DB->open("","scott","tiger");
  184.     $q = "Select * from scott.emp";
  185.     $test_query = new ora_query($DB,$q);
  186.     while($test_query->getrow())
  187.     {
  188.     echo "<br>".$test_query->row["JOB"];
  189.     }
  190.     $q = "Insert into testtable values(?,?,?,?);
  191.     $data[] = "something";
  192.     $data[] = "something";
  193.     $data[] = "something";
  194.     $data[] = "something";
  195.     $test1_query->prepare($DB,$q);
  196.     $test1_query->execute($DB,$data);
  197.     ?>
  198.  
  199.