Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | None | 0 0
  1. <?php
  2. /*
  3.  * File Name: Database.php
  4.  * Date: November 18, 2008
  5.  * Author: Angelo Rodrigues
  6.  * Description: Contains database connection, result
  7.  *              Management functions, input validation
  8.  *
  9.  *              All functions return true if completed
  10.  *              successfully and false if an error
  11.  *              occurred
  12.  *
  13.  */
  14. class Database
  15. {
  16.  
  17.     /*
  18.      * Edit the following variables
  19.      */
  20.     private $db_host = 'localhost';     // Database Host
  21.     private $db_user = 'root';          // Username
  22.     private $db_pass = 'root';          // Password
  23.     private $db_name = 'blog';          // Database
  24.     /*
  25.      * End edit
  26.      */
  27.  
  28.     private $con = false;               // Checks to see if the connection is active
  29.     private $result = array();          // Results that are returned from the query
  30.  
  31.     /*
  32.      * Connects to the database, only one connection
  33.      * allowed
  34.      */
  35.     public function connect()
  36.     {
  37.         if(!$this->con)
  38.         {
  39.             $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
  40.             if($myconn)
  41.             {
  42.                 $seldb = @mysql_select_db($this->db_name,$myconn);
  43.                 if($seldb)
  44.                 {
  45.                     $this->con = true;
  46.                     return true;
  47.                 }
  48.                 else
  49.                 {
  50.                     return false;
  51.                 }
  52.             }
  53.             else
  54.             {
  55.                 return false;
  56.             }
  57.         }
  58.         else
  59.         {
  60.             return true;
  61.         }
  62.     }
  63.  
  64.     /*
  65.     * Changes the new database, sets all current results
  66.     * to null
  67.     */
  68.     public function setDatabase($name)
  69.     {
  70.         if($this->con)
  71.         {
  72.             if(@mysql_close())
  73.             {
  74.                 $this->con = false;
  75.                 $this->results = null;
  76.                 $this->db_name = $name;
  77.                 $this->connect();
  78.             }
  79.         }
  80.  
  81.     }
  82.  
  83.     /*
  84.     * Checks to see if the table exists when performing
  85.     * queries
  86.     */
  87.     private function tableExists($table)
  88.     {
  89.         $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
  90.         if($tablesInDb)
  91.         {
  92.             if(mysql_num_rows($tablesInDb)==1)
  93.             {
  94.                 return true;
  95.             }
  96.             else
  97.             {
  98.                 return false;
  99.             }
  100.         }
  101.     }
  102.  
  103.     /*
  104.     * Selects information from the database.
  105.     * Required: table (the name of the table)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement