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

  1. Oracle8 abst.layer 
  2.  
  3. A database abstraction layer for the PHP Oracle 8 module (available from PHP 3.0.5). It supports persistent connections, fetching rows into arrays, prepare/execute (variable binding) and has a new and improved error interface. 
  4.  
  5.  
  6. <?php  // -*- C++ -*- 
  7. /* 
  8.  * $Id: db-oci8.phl,v 1.2 1998/10/01 19:16:06 ssb Exp $ 
  9.  */ 
  10.  
  11. $db_error_code = 0; 
  12. $db_error_msg = false; 
  13. $db_error_source = false; 
  14.  
  15. /* 
  16.  * Database specific notes: 
  17.  * 
  18.  * - You must configure Oracle listener to use this abstraction layer. 
  19.  * 
  20.  */ 
  21.  
  22.  
  23. /** 
  24.  * @function db_connect 
  25.  * @purpose Connect to a database 
  26.  * @desc 
  27.  *   Connects to a database and returns and identifier for the connection. 
  28.  * @arg database 
  29.  *   Data source name or database host to connect to. 
  30.  * @arg user 
  31.  *   Name of user to connect as. 
  32.  * @arg password 
  33.  *   The user's password. 
  34.  */ 
  35. function db_connect($database, $user, $password) 
  36.     $ret = @OCILogon($user, $password, $database); 
  37.     db_check_errors($php_errormsg); 
  38.     return $ret; 
  39.  
  40. /* 
  41.  * Function: db_query 
  42.  * Arguments: $conn (int)     - connection identifier 
  43.  *            $query (string) - SQL statement to execute 
  44.  * Description: executes an SQL statement 
  45.  * Returns:   false - query failed 
  46.  *          integer - query succeeded, value is result handle 
  47.  */ 
  48. function db_query($conn, $query) 
  49.     $stmt = @OCIParse($conn, $query); 
  50.     db_check_errors($php_errormsg); 
  51.     if (!$stmt) { 
  52.     return false; 
  53.     } 
  54.     if (@OCIExecute($stmt)) { 
  55.     return $stmt; 
  56.     } 
  57.     db_check_errors($php_errormsg); 
  58.     @OCIFreeStatement($stmt); 
  59.     return false; 
  60.  
  61. /* 
  62.  * Function: db_fetch_row 
  63.  * Arguments: $stmt (int)   - result identifier 
  64.  * Description: Returns an array containing data from a fetched row. 
  65.  * Returns:   false - error 
  66.  *          (array) - returned row, first column at index 0 
  67.  */ 
  68. function db_fetch_row($stmt) 
  69.     $cols = @OCIFetchInto($stmt, &$row); 
  70.     if (!$cols) { 
  71.     db_check_errors($php_errormsg); 
  72.     return false; 
  73.     } 
  74.     return $row; 
  75.  
  76. /* 
  77.  * Function: db_free_result 
  78.  * Arguments: $stmt (int)   - result identifier 
  79.  * Description: Frees all memory associated with a result identifier. 
  80.  * Returns: false - failure 
  81.  *           true - success 
  82.  */ 
  83. function db_free_result($stmt) 
  84.     global $db_oci8_pieces; 
  85.  
  86.     if (isset($db_oci8_pieces[$stmt])) { 
  87.     unset($db_oci8_pieces[$stmt]); 
  88.     } 
  89.     $ret = @OCIFreeStatement($stmt); 
  90.     db_check_errors($php_errormsg); 
  91.     return $ret; 
  92.  
  93. /* 
  94.  * Function: db_disconnect 
  95.  * Arguments: $connection (int) - connection identifier 
  96.  * Description: closes a database connection 
  97.  * Returns: false - failure 
  98.  *           true - success 
  99.  */ 
  100. function db_disconnect($connection) 
  101.     $ret = @OCILogoff($connection); 
  102.     db_check_errors($php_errormsg); 
  103.     return $ret; 
  104.  
  105. /* 
  106.  * Function: db_autocommit 
  107.  * Arguments: $connection (int) - connection identifier 
  108.  * Description: turn autocommit on or off 
  109.  * Returns: false - failure 
  110.  *           true - success 
  111.  */ 
  112. function db_autocommit($connection, $enabled) 
  113.     if (!$enabled) { 
  114.     db_post_error(0,  "Transactions not yet implemented"); 
  115.     return false; 
  116.     } 
  117.     return true; 
  118.  
  119.  
  120. function db_commit($connection) 
  121.     return true; 
  122.  
  123.  
  124. function db_rollback($connection) 
  125.     db_post_error(0,  "Transactions not yet implemented"); 
  126.     return false; 
  127.  
  128.  
  129. function db_quote_string($string) 
  130.     $ret = ereg_replace( "'",  "''", $string); 
  131.     return $ret; 
  132.  
  133.  
  134. function db_prepare($connection, $query) 
  135.     global $db_oci8_pieces; 
  136.  
  137.     $pieces = explode( "?", $query); 
  138.     $new_query =  ""; 
  139.     $last_piece = sizeof($pieces) - 1; 
  140.     print  "<br>last_piece=$last_piece\n"; 
  141.     while (list($i, $piece) = each($pieces)) { 
  142.     $new_query .= $piece; 
  143.     if ($i < $last_piece) { 
  144.         $new_query .=  ":var$i"; 
  145.     } 
  146.     } 
  147.     print  "<br>new_query=$new_query\n"; 
  148.     $stmt = @OCIParse($connection, $new_query); 
  149.     if (!$stmt) { 
  150.     db_check_errors($php_errormsg); 
  151.     return false; 
  152.     } 
  153.     for ($i = 0; $i < $last_piece; $i++) { 
  154.     $bindvar =  ":var$i"; 
  155.     print  "<br>trying to bind $bindvar\n"; 
  156.     if (!@OCIBindByName($stmt, $bindvar, &$db_oci8_pieces[$stmt][$i])) { 
  157.         db_check_errors($php_errormsg); 
  158.         @OCIFreeStatement($stmt); 
  159.         return false; 
  160.     } 
  161.     } 
  162.     return $stmt; 
  163.  
  164.  
  165. function db_execute($stmt, $data) 
  166.     global $db_oci8_pieces; 
  167.  
  168.     while (list($i, $value) = each($data)) { 
  169.     $db_oci8_pieces[$stmt][$i] = $data[$i]; 
  170.     } 
  171.     $ret = @OCIExecute($stmt); 
  172.     if (!$ret) { 
  173.     db_check_errors($php_errormsg); 
  174.     return false; 
  175.     } 
  176.     return true; 
  177.  
  178.  
  179. function db_error_code() 
  180.     global $db_error_code; 
  181.     return $db_error_code; 
  182.  
  183.  
  184. function db_error_msg() 
  185.     global $db_error_msg; 
  186.     return $db_error_msg; 
  187.  
  188.  
  189. function db_error_source() 
  190.     global $db_error_source; 
  191.     return $db_error_source; 
  192.  
  193.  
  194. function db_check_errors($errormsg) 
  195.     global $db_error_code, $db_error_msg, $db_error_source; 
  196.     if (ereg( '^([^:]*): (...-.....): (.*)', $errormsg, &$data)) { 
  197.     list($foo, $function, $db_error_code, $db_error_msg) = $data; 
  198.     $db_error_msg =  "$function: $db_error_msg"; 
  199.     $db_error_source =  "[Oracle][PHP][OCI8]"; 
  200.     } elseif (ereg( '^([^:]*): (.*)', $errormsg, &$data)) { 
  201.     list($foo, $function, $db_error_msg) = $data; 
  202.     $db_error_msg =  "$function: $db_error_msg"; 
  203.     $db_error_code = 0; 
  204.     $db_error_source =  "[PHP][OCI8][db-oci8]"; 
  205.     } else { 
  206.     $db_error_msg = $errormsg; 
  207.     $db_error_code = 0; 
  208.     $db_error_source =  "[PHP][OCI8][db-oci8]"; 
  209.     } 
  210.  
  211.  
  212. function db_post_error($code, $message) 
  213.     global $db_error_code, $db_error_msg, $db_error_source; 
  214.     $db_error_code = $code; 
  215.     $db_error_msg = $message; 
  216.     $db_error_source =  "[PHP][OCI8][db-oci8]"; 
  217.  
  218.  
  219. function db_api_version() 
  220.     return 10;  // 1.0 
  221.  
  222. ?> 
  223.  
  224.