irwan

PHP - Database Interface Class

Nov 12th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * File: Database.php
  4.  *
  5.  * This class is designed to be the interface class with a database.
  6.  * This is currently setup to use SQL syntax however can be changed if needed in future for a different type of database.
  7.  * Using this class will save a lot of programming if a change was needed.
  8.  * Also works brilliantly for MySQL, used in 3 major projects so far :)
  9.  *
  10.  *
  11.  * @author     Daniel Rosser
  12.  * @version    1.0
  13.  *
  14.  */
  15.  
  16.  
  17. class Database {
  18.     private $HOST  = "example.com";
  19.     private $TABLE = "table";
  20.     private $USER  = "";
  21.     private $PASS  = "";
  22.     private $ERROR_CONNECT = "Could not connect to Database";
  23.     private $ERROR_QUERY   = "Could not query the Database";
  24.     private $connection;
  25.     private $lastResult;
  26.    
  27.     /**
  28.      * Constructor
  29.      * @param type - Type of connection, Read only, Write
  30.      */
  31.     function __construct($type)  //php 5+ style constructor
  32.     {
  33.       switch ($type) {    
  34.       case readWrite:
  35.           $this->USER = "username";
  36.           $this->PASS = "password";
  37.           break;
  38.       case readWriteCreate:
  39.           $this->USER = "username";
  40.           $this->PASS = "password";
  41.           break;
  42.       case readOnly:
  43.       default:
  44.           $this->USER = "username";
  45.           $this->PASS = "password";
  46.           break;
  47.        }      
  48.        $this->connect();
  49.     }
  50.    
  51.     /**
  52.      * Destructor
  53.      */
  54.     function __destruct()
  55.     {
  56.        $this->close;
  57.        }
  58.    
  59.     /**
  60.      * Connect to the database
  61.      * @return connection
  62.      */
  63.    
  64.     function connect()
  65.     {
  66.        $this->connection = mysql_connect($this->HOST,$this->USER,$this->PASS) or print mysql_error($this->ERROR_CONNECT);
  67.        $result = mysql_select_db($this->TABLE,$this->connection);
  68.        return($connection);
  69.     }  
  70.    
  71.     /**
  72.      * Delete a row from a table
  73.      * @param table - Table name
  74.      * @param id - ID of row
  75.      * @return result
  76.      */    
  77.     function delete($table, $id)
  78.     {
  79.        $query = "DELETE FROM $table WHERE id='$id'";
  80.        $result = mysql_query($query,$this->connection) or print mysql_error($this->ERROR_QUERY);
  81.        return($result);
  82.     }
  83.    
  84.     /**
  85.      * Delete a row from a table
  86.      * @param table - Table name
  87.      * @param idName - name of the ID row
  88.      * @param id - ID of row
  89.      * @return result
  90.      */
  91.     function deleteSpecific($table, $idName, $id)
  92.     {
  93.        $query = "DELETE FROM $table WHERE $idName='$id'";
  94.        $result = mysql_query($query,$this->connection) or print mysql_error($this->ERROR_QUERY);
  95.        return($result);
  96.     }
  97.    
  98.     /**
  99.      * Disconnects and closes the connection with database
  100.      */
  101.     function disconnect()
  102.     {
  103.        mysql_close($this->connection);
  104.     }
  105.    
  106.     /**
  107.      * Insert fields, values into Table
  108.      * @param table - Table name
  109.      * @param fields - Fields to insert
  110.      * @param values - Values to insert
  111.      */
  112.     function insert( $table, $fields, $values )
  113.     {
  114.       $start  = "INSERT INTO $table(";
  115.       $middle = ") VALUES (";
  116.       $end    = ")";
  117.       $size   = sizeof($fields); // verify the size of the fields
  118.       $stringFields = "";
  119.       for($i=0;$i <= ($size-1);$i++){
  120.         $stringFields .= "$fields[$i]";
  121.         if( $i != ($size-1) ){
  122.         $stringFields .= ",";
  123.         }
  124.       }
  125.       $stringValues="";
  126.       for( $k=0; $k <= ($size-1); $k++ ){
  127.           $stringValues .= "\"$values[$k]\"";
  128.           if( $k != ($size-1) ){
  129.             $stringValues .= ",";
  130.           }
  131.        }
  132.       $insert = "$start$stringFields$middle$stringValues$end";      
  133.       $insert = str_replace('""', mysql_escape_string("NULL"), $insert);
  134.       $insert = str_replace('" "', mysql_escape_string("NULL"), $insert);
  135.       $insert= str_replace("''", "", $insert);
  136.       $insert = str_replace("' '", "", $insert);    
  137.       $this->query($insert);
  138.     }
  139.    
  140.     /**
  141.      * Update fields, values into Table
  142.      * @param table - Table name
  143.      * @param fields - Fields to insert
  144.      * @param values - Values to insert
  145.      * @param idName - Name of Primary Key row
  146.      * @param id - The unique identifier
  147.      */
  148.     function update( $table, $fields, $values, $idName, $id )
  149.     {
  150.       $start  = "UPDATE $table SET ";
  151.       $end    = " WHERE $idName=\"$id\";";
  152.       $size   = sizeof($fields); // verify the size of the fields
  153.       $sizeV  = sizeof($values);
  154.       if($sizeV != $size)
  155.         echo "Developer Error, Size of Fields / Values are not the same- Fields:$size Values:$sizeV";
  156.        
  157.       $string = "";      
  158.       for($i=0;$i <= ($size-1);$i++){
  159.         $string .= "$fields[$i] = \"$values[$i]\"";
  160.         if( $i != ($size-1) ){
  161.         $string .= " , ";
  162.         }
  163.       }
  164.       $update = "$start$string$end";        
  165.       $update = str_replace('""', mysql_escape_string("NULL"), $update);
  166.       $update = str_replace('" "', mysql_escape_string("NULL"), $update);
  167.       $update = str_replace("''", "", $update);
  168.       $update = str_replace("' '", "", $update);      
  169.       $this->query($update);
  170.     }
  171.    
  172.     /**
  173.      * Use this for a SQL Query
  174.      * Data is stored inside $this->lastResult
  175.      * @param sql - SQL to query the database
  176.      */
  177.     function query($sql)
  178.     {
  179.       if(!($this->lastResult = mysql_query($sql, $this->connection)))
  180.         die("MySQL Error from Query. Error: ".mysql_error()." From SQL : $sql");
  181.     }
  182.    
  183.     /**
  184.      * Fetch rows from last query
  185.      * @return row array
  186.      */
  187.     function fetchRow()
  188.     {
  189.         return mysql_fetch_array($this->lastResult);
  190.     }
  191.    
  192.     /**
  193.      * Fetch rows from last query
  194.      * @return an associative array that corresponds to the fetched row and moves the internal data pointer ahead
  195.      */
  196.     function fetchRowAssociative()
  197.     {
  198.         return mysql_fetch_assoc($this->lastResult);
  199.     }
  200.    
  201.    
  202.    
  203.     /**
  204.      * Number of rows in a table
  205.      * @param table - Table to query
  206.      */
  207.     function number($table)
  208.     {
  209.         $query  = "SELECT * FROM $table";
  210.         $result = mysql_query($query, $this->connection);
  211.         $number = mysql_num_rows($result);
  212.         return($number);
  213.     }
  214.    
  215.     /**
  216.      * Return the last id of an insertion
  217.      * @return last id of an insertion
  218.      */
  219.     function lastId()
  220.     {
  221.        return mysql_insert_id($this->connection);
  222.     }
  223.    
  224.     /**
  225.      * Return the number of rows affected on last query
  226.      * @return number of rows
  227.      */
  228.     function numberAffected()
  229.     {
  230.        $query = mysql_affected_rows();
  231.        return($query);
  232.     }
  233.    
  234.     /**
  235.      * Return the number of rows in the last query
  236.      * @return number of rows
  237.      */
  238.     function numberOfRows()
  239.     {    $number = mysql_num_rows($this->lastResult);
  240.         return($number);
  241.     }
  242.        
  243.     /**
  244.      * Return the number of rows found in a query
  245.      * @return number of rows
  246.      */
  247.     function numberRows($query)
  248.     {
  249.       $result = mysql_query($query, $this->connection);
  250.       $number = mysql_num_rows($result);
  251.       return($number);
  252.     }  
  253.    
  254. };
  255.  
  256. ?>
  257.  
Advertisement
Add Comment
Please, Sign In to add comment