Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2.  
  3. class Database extends Debug
  4. {
  5.     /**
  6.      * MYSQL server.
  7.      *
  8.      * @access private
  9.      * @var    string   $server
  10.      */
  11.     private $server;
  12.  
  13.     /**
  14.      * MSQL login name.
  15.      *
  16.      * @access private
  17.      * @var    string   $login
  18.      */
  19.     private $login;
  20.  
  21.     /**
  22.      * MYSQL login password.
  23.      *
  24.      * @access private
  25.      * @var    string   $password
  26.      */
  27.     private $password;
  28.  
  29.     /**
  30.      * MYSQL database.
  31.      *
  32.      * @access private
  33.      * @var    string   $database
  34.      */
  35.     private $database;
  36.  
  37.     /**
  38.      * Constructor.
  39.      *
  40.      * @access public
  41.      * @param  string  $server    mysql server
  42.      * @param  string  $login     mysql loginname
  43.      * @param  string  $password  mysql password
  44.      * @param  string  $database  mysql database
  45.      */
  46.     public function __construct ($server, $login, $password, $database)
  47.     {
  48.         $id = "__construct";
  49.         $this->debug($id, "input $server");
  50.         $this->debug($id, "input $login");
  51.         $this->debug($id, "input $password");
  52.         $this->debug($id, "input $database");
  53.  
  54.         $this->server   = $server;
  55.         $this->login    = $login;
  56.         $this->password = $password;
  57.         $this->database = $database;
  58.  
  59.         parent::__construct("Database");
  60.     }
  61.  
  62.     /**
  63.      * Connect to mysql server and database.
  64.      *
  65.      * @access public
  66.      */
  67.     public function connect ()
  68.     {
  69.         $handle = mysql_connect($this->server
  70.                                 , $this->login
  71.                                 , $this->password)
  72.         or die("Could not connect to $login@$host ". mysql_error());
  73.  
  74.         if ( !$handle )
  75.         {
  76.             throw new DatabaseException("Can not connect to database");
  77.         }
  78.  
  79.         $dbSelected = mysql_select_db($this->database)
  80.         or die("Could not select database $database " . mysql_error());
  81.  
  82.         if ( !dbSelect )
  83.         {
  84.             throw new DatabaseException("Could not select database $database");
  85.         }
  86.     }
  87. /**
  88.      * Execute sql query.
  89.      *
  90.      * @access public
  91.      * @param  string  $sql  sql query
  92.      * @return boolean true if there is no error
  93.      */
  94.     public function execute ($sql)
  95.     {
  96.         $id = "execute";
  97.         $this->debug($id, "input $sql");
  98.  
  99.         $resource = mysql_query($sql);
  100.  
  101.         if ( !$resource )
  102.         {
  103.             throw new DatabaseException("Can't execute $sql");
  104.         }
  105.  
  106.         $this->debug($id, "[output: $resource]");
  107.  
  108.         return $resource;
  109.     }
  110.  
  111.     /**
  112.      * Close mysql connection.
  113.      *
  114.      * @access public
  115.      */
  116.     public function close ()
  117. {
  118.         mysql_close();
  119.     }
  120. }
  121.  
  122. // End of file
  123.  
  124. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement