Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*******************************************************************************/
- /* Purpose: This file is all the database connectivity stuff that requires */
- /* some type of error checking. I've made this to hold the username */
- /* and password stuff in a single place while also handling the */
- /* errors. */
- /*******************************************************************************/
- /* Mod History: 11/04/08 */
- /* Added header to this file for proper tracking and documentation of changes */
- /* to the file. */
- /*******************************************************************************/
- /* Mod History: 11/04/08 */
- /* Added include to the config file for preset constants to allow switching of */
- /* databases from PostGres to MySQL. */
- /*******************************************************************************/
- /* Mod History: 11/04/08 */
- /* Added logic to allow the correct calls to the DB connection and queries to */
- /* allow seamless switching of databases from PostGres to MySQL. */
- /*******************************************************************************/
- /* Mod History: 11/05/08 */
- /* Added functions dbExec, dbFetch, dbNRows, dbRes to allow for query actions */
- /* and/or results independent of the database and database functions being */
- /* called whether PostGres or MySQL. */
- /*******************************************************************************/
- //include 'config.php'; // Added 11/04/08
- class dio() {
- var $dbname;
- var $engine;
- var $host;
- var $port;
- var $pwd;
- var $table;
- var $uid;
- function dbConnect() {
- /*******************************************************************/
- /* Function: dbConnect() */
- /* Purpose: Connects to the database and reports on error. */
- /*******************************************************************/
- //echo extension_loaded('pgsql'); // Test for PostGres
- if ($engine=="PostGres") {
- $con_str = "host=$host port=$port user=$uid password=$pwd ".
- "dbname=$dbname";
- pg_connect($con_str) or die("Error: Failed to connect to database");
- } else {
- $conn = mysql_connect($host,$uid,$pwd) or die(mysql_error());
- mysql_select_db($dbname,$conn) or die(mysql_error());
- } // end if DB_ENG else
- } // end function dbConnect
- function dbExec($sql) {
- /***************************************************************************/
- /* Function: dbRowRes() */
- /* Purpose: Queries the database with the given query string and returns */
- /* the results or reports on error. */
- /***************************************************************************/
- if (DB_ENG=="PostGres") {
- $result = pg_exec($sql);
- } else {
- $result = mysql_query($sql);
- } // end if DB_ENG else
- return $result;
- } // end function
- function dbFetch($res,$rw) {
- /***************************************************************************/
- /* Function: dbFetch() */
- /* Purpose: Queries the database with the given query string and returns */
- /* the results or reports on error. */
- /***************************************************************************/
- if (DB_ENG=="PostGres") {
- $result = pg_fetch_row($res, $rw);
- } else {
- $result = mysql_result($res, $rw);
- } // end if DB_ENG else
- print_r($result);
- return $result;
- } // end function
- function dbNRows($res) {
- /***************************************************************************/
- /* Function: dbRowRes() */
- /* Purpose: Queries the database with the given query string and returns */
- /* the results or reports on error. */
- /***************************************************************************/
- echo "Rows=> $res <br>";
- if (DB_ENG=="PostGres") {
- $result = pg_numrows($res);
- } else {
- $result = mysql_num_rows($res);
- } // end if DB_ENG else
- return $result;
- } // end function
- function dbQuery($_query) {
- /***************************************************************************/
- /* Function: dbQuery() */
- /* Purpose: Queries the database with the given query string and returns */
- /* the results or reports on error. */
- /***************************************************************************/
- echo "QryStr=> $_query <br>";
- if (DB_ENG=="PostGres") {
- $_result = pg_query($_query)
- or die("Error: Query failed with query string:<BR>$_query");
- } else {
- // $_result = mysql_query($_query) or die(mysql_error());
- $_result = mysql_query($_query);
- } // end if DB_ENG else
- if ($_result == false) {
- $newquery = sprintf("insert into qry_errors (error_desc) values ('%s')", addslashes($_query));
- if (DB_ENG=="PostGres") {
- $_result1 = pg_query($newquery)
- or die("\n\nfailed the log query insert");
- } else {
- $_result1 = mysql_query($newquery) or die(mysql_error());
- mysql_query("COMMIT");
- } // end if DB_ENG else
- } // end if $_result
- if (dbNRows($_result)==0) {
- return;
- // return -1;
- } else {
- return $_result;
- }
- } // end function
- function dbRes($res,$rw,$col) {
- /***************************************************************************/
- /* Function: dbRowRes() */
- /* Purpose: Queries the database with the given query string and returns */
- /* the results or reports on error. */
- /***************************************************************************/
- echo "SqlStr=> $_query <br>";
- if (DB_ENG=="PostGres") {
- $result = pg_result($res, $rw, $col);
- } else {
- $result = mysql_result($res, $rw, $col);
- } // end if DB_ENG else
- //print_r($result);
- return $result;
- } // end function
- } // end class dio
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement