Guest User

Untitled

a guest
May 31st, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1. <?php
  2.     /***
  3.      * Class: Sql
  4.      * Version: 0.01
  5.      *
  6.      * Desc:
  7.      * This is a simple class for managing mySQL connections.
  8.      * I don't want this to do more then return the query though.
  9.      * Formatting the query will be left to another class!
  10.      *
  11.      * The private connected bool is used for error checking in the class.
  12.      */
  13.     class Sql{
  14.         private $Connected = false;
  15.  
  16.         /***
  17.          * Protected function: isConnected
  18.          * Version: 0.01
  19.          *
  20.          * Desc:
  21.          * This simply returns the private Connected bools value.
  22.          */
  23.         protected function isConnected(){
  24.             return $this->Connected;
  25.         }
  26.        
  27.         /***
  28.          * Function: doQuery
  29.          * Version: 0.01
  30.          *
  31.          * Desc:
  32.          * This function will return the data from the query,
  33.          * if the constructor managed to set up a connection.
  34.          * Otherwise it'll give out an error.
  35.          */
  36.         public function doQuery($queryString){
  37.             if(!$this->isConnected()){
  38.                 return "ERROR no.1: Couldn't connect to DB.";
  39.             }else{
  40.                 return "we got through!";
  41.             }
  42.         }
  43.  
  44.         /***
  45.          * Function: __construct
  46.          * Version: 0.01
  47.          *
  48.          * Desc:
  49.          * This is the initializer of the class. It sets up some mySQL values
  50.          * and then tries to establish a connection.
  51.          * I should add error checking for the @mysql_connect and mysql_select_db.
  52.          */
  53.         function __construct(){
  54.             $str_dbhost=""; //the database host name
  55.             $str_dbuser=""; //the database user
  56.             $str_dbname=""; //the database name
  57.             $str_dbpass=""; //the database password
  58.  
  59.             $sqlconnection = @mysql_connect($str_dbhost,$str_dbuser,$str_dbpass);
  60.             mysql_select_db($str_dbuser);
  61.             if($sqlconnection){
  62.                 $this->Connected = true;
  63.             }
  64.         }
  65.  
  66.         function __destruct(){
  67.             mysql_close();
  68.             $this->Connected = false;
  69.             echo "connection down.";
  70.         }
  71.     }
  72.  
  73.     $sql = new Sql();
  74. ?>
Add Comment
Please, Sign In to add comment