Advertisement
Guest User

Untitled

a guest
Dec 28th, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.85 KB | None | 0 0
  1. <?php
  2. /* database.class.php
  3.  *
  4.  * the main class to run and execute database queries for ease of reading and use in the
  5.  * rest of the application code
  6.  */
  7.  
  8. $request = basename($_SERVER['REQUEST_URI']);
  9. if ($request == 'database.class.php') { echo 'You cannot access this file directly'; exit(); }
  10.  
  11. class DataBase
  12. {
  13.     var $host;
  14.     var $user;
  15.     var $pass;
  16.     var $name;
  17.     var $link;
  18.     var $error;
  19.     var $query;
  20.     var $rows;
  21.     var $connected;
  22.    
  23.     function DataBase($host, $user, $pass, $name)
  24.     {
  25.         $link = @mysql_connect($host, $user, $pass, true);
  26.         if ($link)
  27.         {
  28.             $select = mysql_select_db($name, $link);
  29.             if ($select)
  30.             {
  31.                 $this->host = $host;
  32.                 $this->user = $user;
  33.                 $this->pass = $name;
  34.                 $this->name = $name;
  35.                 $this->link = $link;
  36.                
  37.                 $this->connected = TRUE;
  38.             }
  39.             else
  40.             {
  41.                 $this->error = mysql_error();
  42.                 $this->connected = FALSE;
  43.             }
  44.         }
  45.         else
  46.         {
  47.             $this->error = 'Error connecting to MySQL Server';
  48.             $this->connected = FALSE;
  49.         }
  50.     }
  51.    
  52.     public function Error()
  53.     {
  54.         return $this->error;
  55.     }
  56.    
  57.     public function Sql_Link()
  58.     {
  59.         return $this->link;
  60.     }
  61.    
  62.     // execute a given query and return true/false on success/fail
  63.     public function run()
  64.     {
  65.         $argv = func_get_args();
  66.         $protected_query = call_user_func_array(array($this, 'Q'), $argv);
  67.         $this->query = $protected_query;
  68.        
  69.         $rs = mysql_query($protected_query, $this->link);
  70.        
  71.         if (!$rs)
  72.         {
  73.             echo mysql_error() . "\n" . $protected_query;
  74.             $this->error = mysql_error();
  75.             return FALSE;
  76.         }
  77.         else
  78.         {
  79.             return TRUE;
  80.         }
  81.     }
  82.    
  83.     // execute a given query and return insert id on success
  84.     public function insert()
  85.     {
  86.         $argv = func_get_args();
  87.         $protected_query = call_user_func_array(array($this, 'Q'), $argv);
  88.         $this->query = $protected_query;
  89.        
  90.         $rs = mysql_query($protected_query, $this->link);
  91.         if ($rs) { return mysql_insert_id(); } else { $this->error = mysql_error(); return FALSE; }
  92.     }
  93.    
  94.     // execute a simple mysql_result on a single row
  95.     public function result()
  96.     {
  97.         $argv = func_get_args();
  98.         $protected_query = call_user_func_array(array($this, 'Q'), $argv);
  99.         $this->query = $protected_query;
  100.        
  101.         $rs = mysql_query($protected_query, $this->link);
  102.        
  103.         if (!$rs)
  104.         {
  105.             $this->error = mysql_error();
  106.             return FALSE;
  107.         }
  108.        
  109.         if (mysql_num_rows($rs) > 0)
  110.         {
  111.             $return = mysql_result($rs, 0);
  112.         }
  113.         else
  114.         {
  115.             $return = FALSE;
  116.         }
  117.         return $return;
  118.     }
  119.    
  120.     public function assoc()
  121.     {
  122.         $argv = func_get_args();
  123.         $protected_query = call_user_func_array(array($this, 'Q'), $argv);
  124.         $this->query = $protected_query;
  125.        
  126.         $rs = mysql_query($protected_query, $this->link);
  127.        
  128.         if (!$rs)
  129.         {
  130.             $this->error = mysql_error();
  131.             return false;
  132.         }
  133.        
  134.         if (mysql_num_rows($rs) == 0)
  135.         {
  136.             $this->rows = mysql_num_rows($rs);
  137.             return false;
  138.         }
  139.        
  140.         $return = array();
  141.         if (mysql_num_rows($rs) == 1)
  142.         {
  143.             $return = mysql_fetch_assoc($rs);
  144.         }
  145.         else
  146.         {
  147.             while ($entry = mysql_fetch_assoc($rs))
  148.             {
  149.                 $return[] = $entry;
  150.             }
  151.         }
  152.         return $return;
  153.     }
  154.    
  155.     public function force_multi_assoc()
  156.     {
  157.         $argv = func_get_args();
  158.         $protected_query = call_user_func_array(array($this, 'Q'), $argv);
  159.         $this->query = $protected_query;
  160.        
  161.         $rs = mysql_query($protected_query, $this->link);
  162.         if (!$rs)
  163.         {
  164.             $this->error = mysql_error();
  165.             return FALSE;
  166.         }
  167.         if (mysql_num_rows($rs) == 0) { return false; }
  168.        
  169.         $return = array();
  170.         while ($entry = mysql_fetch_assoc($rs))
  171.         {
  172.             $return[] = $entry;
  173.         }
  174.         return $return;
  175.     }
  176.    
  177.     private function Q($_query)
  178.     {
  179.         $argv = func_get_args();
  180.         $argc = func_num_args();
  181.         $n = 1;         // first vararg $argv[1]
  182.  
  183.         $out = '';
  184.         $quote = FALSE;     // quoted string state
  185.         $slash = FALSE;     // backslash state
  186.  
  187.         // b - pointer to start of uncopied text
  188.         // e - pointer to current input character
  189.         // end - end of string pointer
  190.         $end = strlen($_query);
  191.         for ($b = $e = 0; $e < $end; ++$e)
  192.         {
  193.             $ch = $_query{$e};
  194.  
  195.             if ($quote !== FALSE)
  196.             {
  197.                 if ($slash)
  198.                 {
  199.                     $slash = FALSE;
  200.                 }
  201.                 elseif ($ch === '\\')
  202.                 {
  203.                     $slash = TRUE;
  204.                 }
  205.                 elseif ($ch === $quote)
  206.                 {
  207.                     $quote = FALSE;
  208.                 }
  209.             }
  210.             elseif ($ch === "'" || $ch === '"')
  211.             {
  212.                 $quote = $ch;
  213.             }
  214.             elseif ($ch === '?')
  215.             {
  216.                 $out .= substr($_query, $b, $e - $b) .
  217.                     $this->_Q_escape($argv[$n], $n);
  218.                 $b = $e + 1;
  219.                 $n++;
  220.             }
  221.         }
  222.         $out .= substr($_query, $b, $e - $b);
  223.  
  224.         // warn on arg count mismatch
  225.         if ($argc != $n)
  226.         {
  227.             $adj = ($argc > $n) ? 'many' : 'few';
  228.             trigger_error('Too ' . $adj . ' arguments ' .
  229.                     '(expected ' . $n . '; got ' . $argc . ')',
  230.                 E_USER_WARNING);
  231.         }
  232.  
  233.         return $out;
  234.     }
  235.  
  236.     private function _Q_escape($_value, $_position = FALSE)
  237.     {
  238.         static $r_position;
  239.  
  240.         // Save $_position to simplify recursive calls.
  241.         if ($_position !== FALSE)
  242.         {
  243.             $r_position = $_position;
  244.         }
  245.  
  246.         if (is_null($_value))
  247.         {
  248.             // The NULL value
  249.             return 'NULL';
  250.         }
  251.         elseif (is_int($_value) || is_float($_value))
  252.         {
  253.             // All integer and float representations should be
  254.             // safe for mysql (including 5e-12 notation)
  255.             $result = "$_value";
  256.         }
  257.         elseif (is_array($_value))
  258.         {
  259.             // Arrays are written as a comma-separated list of
  260.             // values.  Useful for IN, find_in_set(), etc.
  261.  
  262.             // KM, AS: PHP stoneage is crashing here, when the
  263.             // _values array is missing a 0 index.. hence the array_values()
  264.             $result = implode(', ', array_map(array($this, '_Q_escape'), array_values($_value)));
  265.         }
  266.         else
  267.         {
  268.             // Warn if given an unexpected value type
  269.             if (!is_string($_value))
  270.             {
  271.                 trigger_error('Unexpected value of type "' .
  272.                     gettype($_value) . '" in arg '.$r_position,
  273.                     E_USER_WARNING);
  274.             }
  275.  
  276.             // Everything else gets escaped as a string
  277.             $result = "'" . addslashes($_value) . "'";
  278.         }
  279.  
  280.         return $result;
  281.     }
  282. }
  283.  
  284.  
  285. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement