Advertisement
TBotNik

em_dio.php

Jun 14th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.63 KB | None | 0 0
  1. <?php
  2. /*******************************************************************************/
  3. /* Purpose: This file is all the database connectivity stuff that requires     */
  4. /*          some type of error checking.  I've made this to hold the username  */
  5. /*          and password stuff in a single place while also handling the       */
  6. /*          errors.                                                            */
  7. /*******************************************************************************/
  8. /* Mod History: 11/04/08                                                       */  
  9. /* Added header to this file for proper tracking and documentation of changes  */
  10. /* to the file.                                                                */
  11. /*******************************************************************************/
  12. /* Mod History: 11/04/08                                                       */  
  13. /* Added include to the config file for preset constants to allow switching of */
  14. /* databases from PostGres to MySQL.                                           */
  15. /*******************************************************************************/
  16. /* Mod History: 11/04/08                                                       */  
  17. /* Added logic to allow the correct calls to the DB connection and queries to  */
  18. /* allow seamless switching of databases from PostGres to MySQL.               */
  19. /*******************************************************************************/
  20. /* Mod History: 11/05/08                                                       */  
  21. /* Added functions dbExec, dbFetch, dbNRows, dbRes to allow for query actions  */
  22. /* and/or results independent of the database and database functions being     */
  23. /* called whether PostGres or MySQL.                                           */
  24. /*******************************************************************************/
  25.    //include 'config.php';                  // Added 11/04/08
  26.  
  27.    class dio() {
  28.     var $dbname;
  29.     var $engine;
  30.     var $host;
  31.     var $port;
  32.     var $pwd;
  33.     var $table;
  34.     var $uid;
  35.  
  36.     function dbConnect() {
  37.        /*******************************************************************/
  38.        /*  Function: dbConnect()                                          */
  39.        /*   Purpose: Connects to the database and reports on error.       */
  40.        /*******************************************************************/
  41.           //echo extension_loaded('pgsql'); // Test for PostGres
  42.           if ($engine=="PostGres") {
  43.          $con_str = "host=$host port=$port user=$uid password=$pwd ".
  44.                     "dbname=$dbname";
  45.          pg_connect($con_str) or die("Error: Failed to connect to database");
  46.           } else {
  47.          $conn = mysql_connect($host,$uid,$pwd) or die(mysql_error());
  48.          mysql_select_db($dbname,$conn) or die(mysql_error());
  49.           }  // end if DB_ENG else
  50.     }  // end function dbConnect
  51.  
  52.     function dbExec($sql) {
  53.         /***************************************************************************/
  54.         /*  Function: dbRowRes()                                                    */
  55.         /*   Purpose: Queries the database with the given query string and returns */
  56.         /*            the results or reports on error.                             */
  57.         /***************************************************************************/
  58.         if (DB_ENG=="PostGres") {
  59.             $result = pg_exec($sql);
  60.         } else {
  61.             $result = mysql_query($sql);
  62.         }       // end if DB_ENG else
  63.         return $result;
  64.     }           // end function
  65.  
  66.     function dbFetch($res,$rw) {
  67.         /***************************************************************************/
  68.         /*  Function: dbFetch()                                                    */
  69.         /*   Purpose: Queries the database with the given query string and returns */
  70.         /*            the results or reports on error.                             */
  71.         /***************************************************************************/
  72.         if (DB_ENG=="PostGres") {
  73.             $result = pg_fetch_row($res, $rw);
  74.         } else {
  75.             $result = mysql_result($res, $rw);
  76.         }       // end if DB_ENG else
  77.     print_r($result);
  78.         return $result;
  79.         }           // end function
  80.  
  81.     function dbNRows($res) {
  82.         /***************************************************************************/
  83.         /*  Function: dbRowRes()                                                    */
  84.         /*   Purpose: Queries the database with the given query string and returns */
  85.         /*            the results or reports on error.                             */
  86.         /***************************************************************************/
  87.     echo "Rows=> $res <br>";
  88.         if (DB_ENG=="PostGres") {
  89.             $result = pg_numrows($res);
  90.         } else {
  91.             $result = mysql_num_rows($res);
  92.         }       // end if DB_ENG else
  93.         return $result;
  94.     }           // end function
  95.  
  96.     function dbQuery($_query) {
  97.         /***************************************************************************/
  98.         /*  Function: dbQuery()                                                    */
  99.         /*   Purpose: Queries the database with the given query string and returns */
  100.         /*            the results or reports on error.                             */
  101.         /***************************************************************************/
  102.     echo "QryStr=> $_query <br>";
  103.         if (DB_ENG=="PostGres") {
  104.             $_result = pg_query($_query)
  105.             or die("Error: Query failed with query string:<BR>$_query");
  106.         } else {
  107.     //      $_result = mysql_query($_query) or die(mysql_error());
  108.             $_result = mysql_query($_query);
  109.         }       // end if DB_ENG else
  110.         if ($_result == false) {
  111.             $newquery = sprintf("insert into qry_errors (error_desc) values ('%s')", addslashes($_query));  
  112.             if (DB_ENG=="PostGres") {
  113.                 $_result1 = pg_query($newquery)
  114.                     or die("\n\nfailed the log query insert");
  115.             } else {
  116.                 $_result1 = mysql_query($newquery) or die(mysql_error());
  117.                 mysql_query("COMMIT");     
  118.             }   // end if DB_ENG else
  119.         }       // end if $_result
  120.         if (dbNRows($_result)==0) {
  121.             return;
  122.     //      return -1;
  123.         } else {
  124.             return $_result;
  125.         }
  126.     }           // end function
  127.  
  128.     function dbRes($res,$rw,$col) {
  129.         /***************************************************************************/
  130.         /*  Function: dbRowRes()                                                    */
  131.         /*   Purpose: Queries the database with the given query string and returns */
  132.         /*            the results or reports on error.                             */
  133.         /***************************************************************************/
  134.     echo "SqlStr=> $_query <br>";
  135.         if (DB_ENG=="PostGres") {
  136.             $result = pg_result($res, $rw, $col);
  137.         } else {
  138.             $result = mysql_result($res, $rw, $col);
  139.         }       // end if DB_ENG else
  140.     //print_r($result);
  141.         return $result;
  142.     }           // end function
  143.    }  // end class dio
  144. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement