Public paste
MySQL class
By: fragalot | Date: Apr 26 2009 16:20 | Format: PHP | Expires: never | Size: 2.18 KB | Hits: 1186

  1. <?php
  2. /**
  3.  *
  4.  * MySQL wrapper class
  5.  *
  6.  * @author Kerkhove Thomas
  7.  * @copyright Constellation Computers
  8.  * @version 1.2
  9.  */
  10. if(!defined('C_MYSQL'))
  11. {
  12.         define('C_MYSQL', true);
  13.  
  14.         class _MySQL
  15.         {
  16.                 private $MYSQL_db;
  17.                 public  $connect;
  18.                 private $get_db;
  19.  
  20.                 /**
  21.                  * Constructor, connects to the database.
  22.                  * ( calls $this->Connect() )
  23.                  *
  24.                  */
  25.                 function __construct()
  26.                 {
  27.                 }
  28.  
  29.                 /**
  30.                  * Connects to the database
  31.                  *
  32.                  */
  33.                 function Connect($MYSQL_host, $MYSQL_user, $MYSQL_pass, $MYSQL_db)
  34.                 {
  35.                         try
  36.                         {
  37.                                 $this->connect  = mysql_connect(
  38.                                                         $MYSQL_host,
  39.                                                         $MYSQL_user,
  40.                                                         $MYSQL_pass
  41.                                                 );
  42.                                 $this->get_db   = mysql_select_db($MYSQL_db );
  43.                                 if($this->get_db == FALSE)
  44.                                 {
  45.                                         throw new ErrorException( "Database unavailable", 10, 100);
  46.                                 }
  47.                         }
  48.                         catch(Exception $e)
  49.                         {
  50.                                 die( 'Caught exception: ' . $e->getMessage() );
  51.                         }
  52.                 }
  53.  
  54.                 /**
  55.                  * Perform a query
  56.                  *
  57.                  * @param String $sql
  58.                  * @return resource
  59.                  */
  60.                 function Query( $sql )
  61.                 {
  62.                         $que = mysql_query( $sql, $this->connect ) or die( mysql_error( $this->connect ) );
  63.                         return $que;
  64.                 }
  65.  
  66.                 /**
  67.                  * Perform a query and return assoc array
  68.                  *
  69.                  * @param String $sql
  70.                  * @return array
  71.                  */
  72.                 function Fetch( $sql )
  73.                 {
  74.                         $que = $this->Query( $sql, $this->connect );
  75.                         while( $res = mysql_fetch_assoc( $que ) )
  76.                         {
  77.                                 $array[] = $res;
  78.                         }
  79.                         return $array;
  80.                 }
  81.  
  82.                 /**
  83.                  * Perform a query and return numrows
  84.                  *
  85.                  * @param String $sql
  86.                  * @return int
  87.                  */
  88.                 function Num_Rows( $sql )
  89.                 {
  90.                         return mysql_num_rows( $this->Query( $sql ) );
  91.                 }
  92.  
  93.                 /**
  94.                  * sanitize variables
  95.                  *
  96.                  * @param mixed $input
  97.                  * @param bool $html (allow html?)
  98.                  * @return mixed
  99.                  */
  100.                 function Clean ( $input, $html = FALSE)
  101.                 {
  102.                         if( is_array( $input ) )
  103.                         {
  104.                                 foreach( $input as $key => $value )
  105.                                 {
  106.                                         $output[$key] = $this->Clean( $value );
  107.                                 }
  108.                         }
  109.                         else
  110.                         {
  111.                                 if( get_magic_quotes_gpc() )
  112.                                 {
  113.                                         $input = stripslashes( $input );
  114.                                 }
  115.                                 $output = mysql_real_escape_string( $input );
  116.                                 if( !$html )
  117.                                 {
  118.                                         $output = htmlentities( $output );
  119.                                 }
  120.                         }
  121.  
  122.                         return $output;
  123.                 }
  124.         }
  125. }
  126. ?>