Advertisement
Guest User

MySQL Connection Class

a guest
Nov 28th, 2012
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.82 KB | None | 0 0
  1. <?
  2.  
  3. class MySQL {
  4.    
  5.     /**
  6.      * The DB Host to connect to
  7.      *
  8.      * @var string
  9.      */
  10.     protected $host;
  11.    
  12.     /**
  13.      * The DB Username to access the host
  14.      *
  15.      * @var string
  16.      */
  17.     protected $username;
  18.    
  19.     /**
  20.      * The DB Password to access the host
  21.      *
  22.      * @var string
  23.      */
  24.     protected $password;
  25.    
  26.     /**
  27.      * The DB Name to use once connected
  28.      *
  29.      * @var string
  30.      */
  31.     protected $database;
  32.    
  33.     /**
  34.      * The connection string once the connection is made
  35.      *
  36.      * @var resource
  37.      */
  38.     protected $connection;
  39.    
  40.     protected $debug=0;
  41.    
  42.     /**
  43.      * The constructor to create a MySQL Database object
  44.      *
  45.      * @param string $host
  46.      * @param string $username
  47.      * @param string $password
  48.      * @param string $database
  49.      */
  50.     function __construct($host, $username, $password, $database){
  51.         $this->host = $host;
  52.         $this->username = $username;
  53.         $this->password = $password;
  54.         $this->database = $database;
  55.  
  56.         $this->connection = @mysql_connect($this->host, $this->username, $this->password);
  57.    
  58.         if(!$this->connection){
  59.             print "There was an error while connecting to the database.  Please verify that your settings are correct and that your database allows connections from this server.";
  60.             print "<br />" . mysql_error();
  61.             exit;
  62.         }
  63.        
  64.         $this->selectDatabase();
  65.    
  66.     }
  67.    
  68.     /**
  69.      * Sets the debug flag
  70.      *
  71.      * @param int $debug
  72.      */
  73.     function setDebug($debug){
  74.         $this->debug = $debug;
  75.     }
  76.    
  77.    
  78.    
  79.     function execute($query) {
  80.        
  81.         if($this->debug){
  82.             print "<b>QUERY: $query</b><br />\n";
  83.         }
  84.        
  85.         $result = mysql_query($query);
  86.        
  87.         if(!$result) return(null);
  88.        
  89.         $rows = array();
  90.         $fields = array();
  91.        
  92.         while (@$row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  93.  
  94.             while($field = key($row)) {
  95.                 $fields[$field] = $row[$field];
  96.                 next($row);
  97.             }
  98.             $rows[] = $fields;
  99.  
  100.         }
  101.        
  102.         return($rows);
  103.        
  104.     }
  105.    
  106.     /**
  107.      * Run a select query on the database
  108.      *
  109.      * @param string $query
  110.      * @param string[] $columns An array of column names you want to name the multi-dimenstional array of selected values
  111.      * @return string[][]
  112.      */
  113.     function select($query, $columns){
  114.        
  115.         if($this->debug){
  116.             print "<b>QUERY: $query</b><br />\n";
  117.         }
  118.        
  119.         $result = mysql_query($query);
  120.        
  121.         if(!$result){
  122.             return(null);
  123.         }
  124.        
  125.         $results = array();
  126.         $i=0;
  127.         while ($line = mysql_fetch_array($result, MYSQL_NUM)) {
  128.  
  129.             for($j=0; $j<sizeof($line); $j++){
  130.                 $results[$i][$columns[$j]] = $line[$j];
  131.             }
  132.  
  133.             $i++;
  134.         }
  135.        
  136.         return($results);
  137.     }
  138.    
  139.     /**
  140.      * Run a insert query on the database
  141.      *
  142.      * @param string $query
  143.      * @return int the last inserted id from the query
  144.      */
  145.     function insert($query){
  146.        
  147.         if($this->debug){
  148.             print "<b>QUERY: $query</b><br />\n";
  149.         }
  150.        
  151.         $result = mysql_query($query);
  152.        
  153.         if(!$result){
  154.             return(null);
  155.         }
  156.        
  157.         $id = mysql_insert_id($this->connection);
  158.        
  159.         return($id);
  160.     }
  161.    
  162.     /**
  163.      * Run an update query on the database
  164.      *
  165.      * @param string $query
  166.      * @return int the number of rows affected
  167.      */
  168.     function update($query){
  169.         if($this->debug){
  170.             print "<b>QUERY: $query</b><br />\n";
  171.         }
  172.        
  173.         $result = mysql_query($query);
  174.        
  175.         if(!$result){
  176.             return(null);  
  177.         }
  178.        
  179.         $id = mysql_affected_rows($this->connection);
  180.        
  181.         return($id);
  182.     }
  183.    
  184.     /**
  185.      * Run a delete query on the database
  186.      *
  187.      * @param string $query
  188.      * @return int the number of rows affected
  189.      */
  190.     function delete($query){
  191.         if($this->debug){
  192.             print "<b>QUERY: $query</b><br />\n";
  193.         }
  194.        
  195.         $result = mysql_query($query);
  196.        
  197.         if(!$result){
  198.             return(null);  
  199.         }
  200.        
  201.         $id = mysql_affected_rows($this->connection);
  202.        
  203.         return($id);
  204.     }
  205.    
  206.     /**
  207.      * Select the database this connection is currently using
  208.      *
  209.      */
  210.     function selectDatabase(){
  211.         if(!@mysql_select_db($this->database)){
  212.             print "There was an error while selecting the database.  Please verify that your settings are correct and that your database allows connections from this server.";
  213.             exit;
  214.         }
  215.     }
  216.    
  217.     /**
  218.      * The destructor used to close the connection on script exit
  219.      *
  220.      */
  221.     function __destruct(){
  222.         if($this->connection){
  223.             mysql_close($this->connection);
  224.         }
  225.     }
  226.    
  227.     /**
  228.      * Access the DB host
  229.      *
  230.      * @return string
  231.      */
  232.     function getHost(){
  233.         return($this->host);
  234.     }
  235.    
  236.     /**
  237.      * Access the DB username
  238.      *
  239.      * @return string
  240.      */
  241.     function getUsername(){
  242.         return($this->username);
  243.     }
  244.    
  245.     /**
  246.      * Access the DB password
  247.      *
  248.      * @return string
  249.      */
  250.     function getPassword(){
  251.         return($this->password);
  252.     }
  253.    
  254.     /**
  255.      * Access the DB name
  256.      *
  257.      * @return unknown
  258.      */
  259.     function getDatabase(){
  260.         return($this->database);
  261.     }
  262.    
  263.     /**
  264.      * Escape a string for mysql
  265.      *
  266.      * @return string
  267.      */
  268.     function escape($string) {
  269.         if (get_magic_quotes_gpc()) {
  270.             $string = stripslashes($string);
  271.         }
  272.         return mysql_real_escape_string($string);
  273.     }
  274. }
  275.  
  276.  
  277. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement