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

  1. ODBC abst.layer 
  2.  
  3. A database abstraction layer for the PHP 3.0 ODBC module. It supports persistent connections, fetching rows into arrays, prepare/execute (variable binding) and has a new and improved error interface. 
  4.  
  5.  
  6.  
  7.  
  8. <?php  // -*- C++ -*- 
  9. /* 
  10.  * $Id: db-odbc.phl,v 1.7 1998/10/01 18:41:56 ssb Exp $ 
  11.  */ 
  12.  
  13. $db_error_code = 0; 
  14. $db_error_msg = false; 
  15. $db_error_source = false; 
  16.  
  17. /** 
  18.  * @function db_connect 
  19.  * @purpose Connect to a database 
  20.  * @desc 
  21.  *   Connects to a database and returns and identifier for the connection. 
  22.  * @arg database 
  23.  *   Data source name or database host to connect to. 
  24.  * @arg user 
  25.  *   Name of user to connect as. 
  26.  * @arg password 
  27.  *   The user's password. 
  28.  */ 
  29. function db_connect($dsn, $user, $password) 
  30.     $ret = @odbc_connect($dsn, $user, $password); 
  31.     if (!$ret) { 
  32.     db_check_errors($php_errormsg); 
  33.     return false; 
  34.     } 
  35.     return $ret; 
  36.  
  37. /* 
  38.  * Function: db_query 
  39.  * Arguments: $conn (int)     - connection identifier 
  40.  *            $query (string) - SQL statement to execute 
  41.  * Description: executes an SQL statement 
  42.  * Returns: (int) 0 - query failed 
  43.  *                1 - query succeeded 
  44.  */ 
  45. function db_query($conn, $query) 
  46.     $ret = @odbc_exec($conn, $query); 
  47.     if (!$ret) { 
  48.     db_check_errors($php_errormsg); 
  49.     return false; 
  50.     } 
  51.     return $ret; 
  52.  
  53. /* 
  54.  * Function: db_fetch_row 
  55.  * Arguments: $result (int)   - result identifier 
  56.  * Description: Returns an array containing data from a fetched row. 
  57.  * Returns:   false - error 
  58.  *          (array) - returned row 
  59.  */ 
  60. function db_fetch_row($result) 
  61.     $row = array(); 
  62.     $cols = @odbc_fetch_into($result, &$row); 
  63.     if (!$cols) { 
  64.     db_check_errors($php_errormsg); 
  65.     return false; 
  66.     } 
  67.     return $row; 
  68.  
  69. /* 
  70.  * Function: db_free_result 
  71.  * Arguments: $result (int)   - result identifier 
  72.  * Description: Frees all memory associated with a result identifier. 
  73.  * Returns: (int) 0 - failure 
  74.  *                1 - success 
  75.  */ 
  76. function db_free_result($result) 
  77.     $ret = @odbc_free_result($result); 
  78.     db_check_errors($php_errormsg); 
  79.     return $ret; 
  80.  
  81. /* 
  82.  * Function: db_disconnect 
  83.  * Arguments: $connection (int) - connection identifier 
  84.  * Description: closes a database connection 
  85.  * Returns: (int) 0 - failure 
  86.  *                1 - success 
  87.  */ 
  88. function db_disconnect($connection) 
  89.     $ret = @odbc_close($connection); 
  90.     db_check_errors($php_errormsg); 
  91.     return $ret; 
  92.  
  93. /* 
  94.  * Function: db_autocommit 
  95.  * Arguments: $connection (int) - connection identifier 
  96.  * Description: turn autocommit on or off 
  97.  * Returns: (int) 0 - failure 
  98.  *                1 - success 
  99.  */ 
  100. function db_autocommit($connection, $enabled) 
  101.     $ret = @odbc_autocommit($connection, $enabled); 
  102.     db_check_errors($php_errormsg); 
  103.     return $ret; 
  104.  
  105.  
  106. function db_commit($connection) 
  107.     $ret = @odbc_commit($connection); 
  108.     db_check_errors($php_errormsg); 
  109.     return $ret; 
  110.  
  111.  
  112. function db_rollback($connection) 
  113.     $ret = @odbc_rollback($connection); 
  114.     db_check_errors($php_errormsg); 
  115.     return $ret; 
  116.  
  117.  
  118. function db_quote_string($string) 
  119.     $ret = ereg_replace( "'",  "''", $string); 
  120.     return $ret; 
  121.  
  122.  
  123. function db_prepare($connection, $query) 
  124.     $ret = @odbc_prepare($connection, $query); 
  125.     db_check_errors($php_errormsg); 
  126.     return $ret; 
  127.  
  128.  
  129. function db_execute($statement, $data) 
  130.     $ret = @odbc_execute($statement, $data); 
  131.     db_check_errors($php_errormsg); 
  132.     return $ret; 
  133.  
  134.  
  135. function db_error_code() 
  136.     global $db_error_code; 
  137.     return $db_error_code; 
  138.  
  139.  
  140. function db_error_msg() 
  141.     global $db_error_msg; 
  142.     return $db_error_msg; 
  143.  
  144.  
  145. function db_error_source() 
  146.     global $db_error_source; 
  147.     return $db_error_source; 
  148.  
  149.  
  150. function db_check_errors($errormsg) 
  151.     global $db_error_code, $db_error_msg, $db_error_source; 
  152.     if (ereg( 'SQL error: (\[.*\]\[.*\]\[.*\])(.*), SQL state (.....)', 
  153.          $errormsg, &$data)) { 
  154.     list($foo, $db_error_source, $db_error_msg, $db_error_code) = $data; 
  155.     } else { 
  156.     $db_error_msg = $db_error_source = false; 
  157.     $db_error_code = 0; 
  158.     } 
  159.  
  160.  
  161. function db_post_error($code, $message) 
  162.     global $db_error_code, $db_error_msg, $db_error_source; 
  163.     $db_error_code = $code; 
  164.     $db_error_msg = $message; 
  165.     $db_error_source =  "[PHP][ODBC][db-odbc]"; 
  166.  
  167.  
  168. function db_api_version() 
  169.     return 10;  // 1.0 
  170.  
  171. ?> 
  172.  
  173.