Advertisement
Guest User

PHP OOP - Mysql

a guest
Aug 3rd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4. * @author     Andre Liebich <[email protected]>
  5. * @copyright  2013 Browserforge
  6. * @link       http://www.browserforge.com
  7. */
  8.  
  9. class Mysql
  10. {
  11.  
  12.     private $host;
  13.     private $username;
  14.     private $password;
  15.     private $database;
  16.    
  17.     /**
  18.     * Create the Variable
  19.     */
  20.  
  21.     function __construct()
  22.     {
  23.         $this -> host = "localhost"; // The Mysql-Host
  24.         $this -> username = "localhost"; // The Username from Database
  25.         $this -> password = "example"; // The Password from Database
  26.         $this -> database = "test"; // Databasename
  27.         Mysql::dbconnect();
  28.     }
  29.    
  30.     /**
  31.     * Create connection to Database
  32.     */
  33.  
  34.     public function dbconnect()
  35.     {
  36.         $connection = mysql_connect($this->host, $this->username, $this->password) or die ("Account not found");
  37.         mysql_select_db($this->database, $connection) or die ("Database not found.");
  38.     }
  39.    
  40.     /**
  41.     * Return the result of the Query like ("INSERT INTO `example`....")
  42.     */
  43.  
  44.     public function query($query)
  45.     {
  46.         $query = mysql_query($query) or die (mysql_error());
  47.         return $query;
  48.     }
  49.    
  50.     /**
  51.     * Return the result of the Query like ("SELECT * FROM `example` WHERE ......")
  52.     */
  53.  
  54.     public function obj($query)
  55.     {
  56.         $query = mysql_fetch_object(mysql_query($query)) or die (mysql_error());
  57.         return $query;
  58.     }
  59.    
  60.     /**
  61.     * Return a number of all results.
  62.     */
  63.  
  64.     public function rows($query)
  65.     {
  66.         $query = mysql_num_rows(mysql_query($query)) or die (mysql_error());
  67.         return $query;
  68.     }
  69.    
  70.     /**
  71.     * Returns an associative array with the field names as keys back
  72.     */
  73.  
  74.     public function assoc($query)
  75.     {
  76.         $query = mysql_fetch_assoc(mysql_query($query)) or die (mysql_error());
  77.         return $query;
  78.     }
  79.  
  80.     /**
  81.     * Return all results in a array
  82.     */
  83.  
  84.     public function arry($query)
  85.     {
  86.         $query = mysql_fetch_array(mysql_query($query)) or die or die (mysql_error());
  87.         return $query;
  88.     }
  89.  
  90.     /**
  91.     * Checks if a table exists
  92.     * @param $table The Name of the Table
  93.     */
  94.  
  95.     function table_exists($table)
  96.     {
  97.         if(mysql_query("SELECT * FROM `$table`")){
  98.             return true;
  99.         }else{
  100.             return false;
  101.         }
  102.     }
  103.    
  104. }
  105.  
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement