Advertisement
Guest User

Untitled

a guest
May 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.42 KB | None | 0 0
  1. <?php
  2. class Database
  3. {
  4.  
  5.     /*
  6.      * Edit the following variables
  7.      */
  8.     private $db_host = 'localhost';     // Database Host
  9.     private $db_user = 'root';          // Username
  10.     private $db_pass = '';          // Password
  11.     private $db_name = 'tongarage';          // Database
  12.     /*
  13.      * End edit
  14.      */
  15.  
  16.     private $con = false;               // Checks to see if the connection is active
  17.     private $result = array();          // Results that are returned from the query
  18.  
  19.     /*
  20.      * Connects to the database, only one connection
  21.      * allowed
  22.      */
  23.     public function connect()
  24.     {
  25.         if(!$this->con)
  26.         {
  27.             $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
  28.             if($myconn)
  29.             {
  30.                 $seldb = @mysql_select_db($this->db_name,$myconn);
  31.                 if($seldb)
  32.                 {
  33.                     $this->con = true;
  34.                     return true;
  35.                 }
  36.                 else
  37.                 {
  38.                     return false;
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 return false;
  44.             }
  45.         }
  46.         else
  47.         {
  48.             return true;
  49.         }
  50.     }
  51.  
  52.     /*
  53.     * Changes the new database, sets all current results
  54.     * to null
  55.     */
  56.     public function setDatabase($name)
  57.     {
  58.         if($this->con)
  59.         {
  60.             if(@mysql_close())
  61.             {
  62.                 $this->con = false;
  63.                 $this->results = null;
  64.                 $this->db_name = $name;
  65.                 $this->connect();
  66.             }
  67.         }
  68.  
  69.     }
  70.  
  71.     /*
  72.     * Checks to see if the table exists when performing
  73.     * queries
  74.     */
  75.     private function tableExists($table)
  76.     {
  77.         $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
  78.         if($tablesInDb)
  79.         {
  80.             if(mysql_num_rows($tablesInDb)==1)
  81.             {
  82.                 return true;
  83.             }
  84.             else
  85.             {
  86.                 return false;
  87.             }
  88.         }
  89.     }
  90.  
  91.     /*
  92.     * Selects information from the database.
  93.     * Required: table (the name of the table)
  94.     * Optional: rows (the columns requested, separated by commas)
  95.     *           where (column = value as a string)
  96.     *           order (column DIRECTION as a string)
  97.     */
  98.     public function select($table, $rows = '*', $where = null, $order = null)
  99.     {
  100.         $q = 'SELECT '.$rows.' FROM '.$table;
  101.         if($where != null)
  102.             $q .= ' WHERE '.$where;
  103.         if($order != null)
  104.             $q .= ' ORDER BY '.$order;
  105.  
  106.         $query = @mysql_query($q);
  107.         if($query)
  108.         {
  109.             $this->numResults = mysql_num_rows($query);
  110.             for($i = 0; $i < $this->numResults; $i++)
  111.             {
  112.                 $r = mysql_fetch_array($query);
  113.                 $key = array_keys($r);
  114.                 for($x = 0; $x < count($key); $x++)
  115.                 {
  116.                     // Sanitizes keys so only alphavalues are allowed
  117.                     if(!is_int($key[$x]))
  118.                     {
  119.                         if(mysql_num_rows($query) > 1)
  120.                             $this->result[$i][$key[$x]] = $r[$key[$x]];
  121.                         else if(mysql_num_rows($query) < 1)
  122.                             $this->result = null;
  123.                         else
  124.                             $this->result[$key[$x]] = $r[$key[$x]];
  125.                     }
  126.                 }
  127.             }
  128.             return true;
  129.         }
  130.         else
  131.         {
  132.             return false;
  133.         }
  134.     }
  135.  
  136.     /*
  137.     * Insert values into the table
  138.     * Required: table (the name of the table)
  139.     *           values (the values to be inserted)
  140.     * Optional: rows (if values don't match the number of rows)
  141.     */
  142.     public function insert($table,$column,$values,$rows = null)
  143.     {
  144.         if($this->tableExists($table))
  145.         {
  146.             $insert = 'INSERT INTO '.$table. '('.$column. ')';
  147.             if($rows != null)
  148.             {
  149.                 $insert .= ' ('.$rows.')';
  150.             }
  151.  
  152.             for($i = 0; $i < count($values); $i++)
  153.             {
  154.                 if(is_string($values[$i]))
  155.                     $values[$i] = '"'.$values[$i].'"';
  156.             }
  157.             $values = implode(',',$values);
  158.             $insert .= ' VALUES ('.$values.')';
  159.  
  160.             $ins = @mysql_query($insert);
  161.  
  162.             if($ins)
  163.             {
  164.                 return true;
  165.             }
  166.             else
  167.             {
  168.                 return false;
  169.             }
  170.         }
  171.     }
  172.  
  173.     /*
  174.     * Deletes table or records where condition is true
  175.     * Required: table (the name of the table)
  176.     * Optional: where (condition [column =  value])
  177.     */
  178.     public function delete($table,$where = null)
  179.     {
  180.         if($this->tableExists($table))
  181.         {
  182.             if($where == null)
  183.             {
  184.                 $delete = 'DELETE '.$table;
  185.             }
  186.             else
  187.             {
  188.                 $delete = 'DELETE FROM '.$table.' WHERE '.$where;
  189.             }
  190.             $del = @mysql_query($delete);
  191.  
  192.             if($del)
  193.             {
  194.                 return true;
  195.             }
  196.             else
  197.             {
  198.                 return false;
  199.             }
  200.         }
  201.         else
  202.         {
  203.             return false;
  204.         }
  205.     }
  206.  
  207.     /*
  208.      * Updates the database with the values sent
  209.      * Required: table (the name of the table to be updated
  210.      *           rows (the rows/values in a key/value array
  211.      *           where (the row/condition in an array (row,condition) )
  212.      */
  213.     public function update($table,$rows,$where)
  214.     {
  215.         if($this->tableExists($table))
  216.         {
  217.             // Parse the where values
  218.             // even values (including 0) contain the where rows
  219.             // odd values contain the clauses for the row
  220.             for($i = 0; $i < count($where); $i++)
  221.             {
  222.                 if($i%2 != 0)
  223.                 {
  224.                     if(is_string($where[$i]))
  225.                     {
  226.                         if(($i+1) != null)
  227.                             $where[$i] = '"'.$where[$i].'" AND ';
  228.                         else
  229.                             $where[$i] = '"'.$where[$i].'"';
  230.                     }
  231.                 }
  232.             }
  233.             $where = implode('',$where);
  234.  
  235.  
  236.             $update = 'UPDATE '.$table.' SET ';
  237.             $keys = array_keys($rows);
  238.             for($i = 0; $i < count($rows); $i++)
  239.             {
  240.                 if(is_string($rows[$keys[$i]]))
  241.                 {
  242.                     $update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
  243.                 }
  244.                 else
  245.                 {
  246.                     $update .= $keys[$i].'='.$rows[$keys[$i]];
  247.                 }
  248.  
  249.                 // Parse to add commas
  250.                 if($i != count($rows)-1)
  251.                 {
  252.                     $update .= ',';
  253.                 }
  254.             }
  255.             $update .= ' WHERE '.$where;
  256.             $query = @mysql_query($update);
  257.             if($query)
  258.             {
  259.                 return true;
  260.             }
  261.             else
  262.             {
  263.                 return false;
  264.             }
  265.         }
  266.         else
  267.         {
  268.             return false;
  269.         }
  270.     }
  271.  
  272.     /*
  273.     * Returns the result set
  274.     */
  275.     public function getResult()
  276.     {
  277.         return $this->result;
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement