topical media & game development

talk show tell print

professional-search-14-seophp-include-database-tools.inc.php / php



  <?php
  // load configuration file
  require_once('config.inc.php');
  
  // database related tools
  class DatabaseTools
  {
    // helper function used to filter data for the database
    function dbIdentifier(str)
    {
      stripped = preg_replace('/[^A-Z0-9_.]/i', '', str);
      tmp = preg_replace('/(.+?)(\.|)/', '`\\1`\\2', stripped);  
      return tmp;
    }
    
    // connect to the database and return the connection handler 
    function getConnection()
    {
      // connect to MySQL server
      db_link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
      
      // throw a 500 error if the database couldn't be reached
      if (db_link === false) 
      {
        header('HTTP/1.0 500 Internal Server Error');
        echo 'Sorry, the Cookie Ogre lost his beloved Cookie Ogress, went bingeing and ate all of our cookies; consequentially, we will be closed until Friday.'; 
      }
      
      // Connect to the seophp database 
      mysql_select_db(DB_DATABASE) or die("Could not select database");
                
      // return the connection handler
      return db_link; 
    }
    
    // close the database connection
    function closeConnection(db_handler)
    {
      mysql_close(db_handler);
    }
  }
  ?>


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.