Mega Code Archive

 
Categories / Php / MySQL Database
 

A database abstraction layer for the PHP 3.0 ODBC module

<?php // -*- C++ -*- /* * $Id: db-odbc.phl,v 1.7 1998/10/01 18:41:56 ssb Exp $ */ $db_error_code = 0; $db_error_msg = false; $db_error_source = false; /** * @function db_connect * @purpose Connect to a database * @desc * Connects to a database and returns and identifier for the connection. * @arg database * Data source name or database host to connect to. * @arg user * Name of user to connect as. * @arg password * The user's password. */ function db_connect($dsn, $user, $password) { $ret = @odbc_connect($dsn, $user, $password); if (!$ret) { db_check_errors($php_errormsg); return false; } return $ret; } /* * Function: db_query * Arguments: $conn (int) - connection identifier * $query (string) - SQL statement to execute * Description: executes an SQL statement * Returns: (int) 0 - query failed * 1 - query succeeded */ function db_query($conn, $query) { $ret = @odbc_exec($conn, $query); if (!$ret) { db_check_errors($php_errormsg); return false; } return $ret; } /* * Function: db_fetch_row * Arguments: $result (int) - result identifier * Description: Returns an array containing data from a fetched row. * Returns: false - error * (array) - returned row */ function db_fetch_row($result) { $row = array(); $cols = @odbc_fetch_into($result, &$row); if (!$cols) { db_check_errors($php_errormsg); return false; } return $row; } /* * Function: db_free_result * Arguments: $result (int) - result identifier * Description: Frees all memory associated with a result identifier. * Returns: (int) 0 - failure * 1 - success */ function db_free_result($result) { $ret = @odbc_free_result($result); db_check_errors($php_errormsg); return $ret; } /* * Function: db_disconnect * Arguments: $connection (int) - connection identifier * Description: closes a database connection * Returns: (int) 0 - failure * 1 - success */ function db_disconnect($connection) { $ret = @odbc_close($connection); db_check_errors($php_errormsg); return $ret; } /* * Function: db_autocommit * Arguments: $connection (int) - connection identifier * Description: turn autocommit on or off * Returns: (int) 0 - failure * 1 - success */ function db_autocommit($connection, $enabled) { $ret = @odbc_autocommit($connection, $enabled); db_check_errors($php_errormsg); return $ret; } function db_commit($connection) { $ret = @odbc_commit($connection); db_check_errors($php_errormsg); return $ret; } function db_rollback($connection) { $ret = @odbc_rollback($connection); db_check_errors($php_errormsg); return $ret; } function db_quote_string($string) { $ret = ereg_replace( "'", "''", $string); return $ret; } function db_prepare($connection, $query) { $ret = @odbc_prepare($connection, $query); db_check_errors($php_errormsg); return $ret; } function db_execute($statement, $data) { $ret = @odbc_execute($statement, $data); db_check_errors($php_errormsg); return $ret; } function db_error_code() { global $db_error_code; return $db_error_code; } function db_error_msg() { global $db_error_msg; return $db_error_msg; } function db_error_source() { global $db_error_source; return $db_error_source; } function db_check_errors($errormsg) { global $db_error_code, $db_error_msg, $db_error_source; if (ereg( 'SQL error: (\[.*\]\[.*\]\[.*\])(.*), SQL state (.....)', $errormsg, &$data)) { list($foo, $db_error_source, $db_error_msg, $db_error_code) = $data; } else { $db_error_msg = $db_error_source = false; $db_error_code = 0; } } function db_post_error($code, $message) { global $db_error_code, $db_error_msg, $db_error_source; $db_error_code = $code; $db_error_msg = $message; $db_error_source = "[PHP][ODBC][db-odbc]"; } function db_api_version() { return 10; // 1.0 } ?>