amin007

troli - inc/mysql.class.php

Mar 4th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.20 KB | None | 0 0
  1. <?php
  2. /**
  3. * MySQL Database Connection Class
  4. * @access public
  5. * @package SPLIB
  6. */
  7. class MySQL
  8. {
  9.     /**
  10.     * MySQL server hostname
  11.     * @access private
  12.     * @var string
  13.     */
  14.     var $host;
  15.  
  16.     /**
  17.     * MySQL username
  18.     * @access private
  19.     * @var string
  20.     */
  21.     var $dbUser;
  22.  
  23.     /**
  24.     * MySQL user's password
  25.     * @access private
  26.     * @var string
  27.     */
  28.     var $dbPass;
  29.  
  30.     /**
  31.     * Name of database to use
  32.     * @access private
  33.     * @var string
  34.     */
  35.     var $dbName;
  36.  
  37.     /**
  38.     * MySQL Resource link identifier stored here
  39.     * @access private
  40.     * @var string
  41.     */
  42.     var $dbConn;
  43.  
  44.     /**
  45.     * Stores error messages for connection errors
  46.     * @access private
  47.     * @var string
  48.     */
  49.     var $connectError;
  50.  
  51.     /**
  52.     * MySQL constructor
  53.     * @param string host (MySQL server hostname)
  54.     * @param string dbUser (MySQL User Name)
  55.     * @param string dbPass (MySQL User Password)
  56.     * @param string dbName (Database to select)
  57.     * @access public
  58.     */
  59.     function MySQL ($host,$dbUser,$dbPass,$dbName)
  60.     {
  61.         $this->host=$host;
  62.         $this->dbUser=$dbUser;
  63.         $this->dbPass=$dbPass;
  64.         $this->dbName=$dbName;
  65.         $this->connectToDb();
  66.     }
  67.  
  68.     /**
  69.     * Establishes connection to MySQL and selects a database
  70.     * @return void
  71.     * @access private
  72.     */
  73.     function connectToDb ()
  74.     {
  75.         // Make connection to MySQL server
  76.         if (!$this->dbConn = @mysql_connect($this->host,$this->dbUser,$this->dbPass))
  77.         {
  78.             trigger_error('Could not connect to server');
  79.             $this->connectError=true;
  80.         // Select database
  81.         }
  82.         else if ( !@mysql_select_db($this->dbName,$this->dbConn) )
  83.         {
  84.             trigger_error('Could not select database');
  85.             $this->connectError=true;
  86.         }
  87.     }
  88.  
  89.     /**
  90.     * Checks for MySQL errors
  91.     * @return boolean
  92.     * @access public
  93.     */
  94.     function isError ()
  95.     {
  96.         if ( $this->connectError )
  97.             return true;
  98.  
  99.         $error=mysql_error ($this->dbConn);
  100.  
  101.         if ( empty ($error) )
  102.             return false;
  103.         else
  104.             return true;
  105.     }
  106.  
  107.     /**
  108.     * Returns an instance of MySQLResult to fetch rows with
  109.     * @param $sql string the database query to run
  110.     * @return MySQLResult
  111.     * @access public
  112.     */
  113.     function query($sql)
  114.     {
  115.         if (!$queryResource=mysql_query($sql,$this->dbConn))
  116.             trigger_error ('Query failed: '.mysql_error($this->dbConn).
  117.                            ' SQL: '.$sql);
  118.         return new MySQLResult($this,$queryResource);
  119.     }
  120. }
  121.  
  122. /**
  123. * MySQLResult Data Fetching Class
  124. * @access public
  125. * @package SPLIB
  126. */
  127. class MySQLResult
  128. {
  129.     /**
  130.     * Instance of MySQL providing database connection
  131.     * @access private
  132.     * @var MySQL
  133.     */
  134.     var $mysql;
  135.  
  136.     /**
  137.     * Query resource
  138.     * @access private
  139.     * @var resource
  140.     */
  141.     var $query;
  142.  
  143.     /**
  144.     * MySQLResult constructor
  145.     * @param object mysql   (instance of MySQL class)
  146.     * @param resource query (MySQL query resource)
  147.     * @access public
  148.     */
  149.     function MySQLResult(& $mysql,$query)
  150.     {
  151.         $this->mysql=& $mysql;
  152.         $this->query=$query;
  153.     }
  154.  
  155.     /**
  156.     * Fetches a row from the result
  157.     * @return array
  158.     * @access public
  159.     */
  160.     function fetch ()
  161.     {
  162.         if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) )
  163.         {
  164.             return $row;
  165.         }
  166.         else if ( $this->size() > 0 )
  167.         {
  168.             mysql_data_seek($this->query,0);
  169.             return false;
  170.         }
  171.         else
  172.         {
  173.             return false;
  174.         }
  175.     }
  176.  
  177.     /**
  178.     * Returns the number of rows selected
  179.     * @return int
  180.     * @access public
  181.     */
  182.     function size ()
  183.     {
  184.         return mysql_num_rows($this->query);
  185.     }
  186.  
  187.     /**
  188.     * Returns the ID of the last row inserted
  189.     * @return int
  190.     * @access public
  191.     */
  192.     function insertID ()
  193.     {
  194.         return mysql_insert_id($this->mysql->dbConn);
  195.     }
  196.    
  197.     /**
  198.     * Checks for MySQL errors
  199.     * @return boolean
  200.     * @access public
  201.     */
  202.     function isError ()
  203.     {
  204.         return $this->mysql->isError();
  205.     }
  206. }
  207. ?>
Advertisement
Add Comment
Please, Sign In to add comment