default
[ class tree: default ] [ index: default ] [ all elements ]

Source for file mySQL.class.php

Documentation is available at mySQL.class.php

  1. <?php    
  2. /**
  3.  * This class handles connections to a mysql database.
  4.  * The class is constructed to make it easy for someone to handle a mysql database
  5.  * 
  6.  * @author Staffan Olin
  7.  * @version 0.2, 05.02.2002
  8.  * @filesource
  9.  * 
  10.  * @modified_by Eric Lamb [eric@ericlamb.net]
  11.  */
  12.  
  13. /**
  14.  * TRUE if you want to be in debug-mode
  15.  * FALSE if not
  16.  */
  17. define("DEBUG"FALSE);
  18.  
  19. /**
  20.  * DO NOT CHANGE THESES VARIABLES
  21.  */
  22.  
  23. /**
  24.  * Command to set the table lock for READ
  25.  */
  26. define("LOCKED_FOR_READ""READ");
  27. /**
  28.  * Command to set the table lock for WRITE
  29.  */
  30. define("LOCKED_FOR_WRITE""WRITE");
  31.  
  32.  
  33. /**
  34.  * HOWTO
  35.  */
  36. class mySQL {
  37.  
  38.     /**
  39.     * The connection resource id
  40.     *
  41.     * @var  object 
  42.     */
  43.     var $connection;
  44.  
  45.     /**
  46.     * The selected database
  47.     *
  48.     * @var  object 
  49.     */
  50.     var $selectedDb;
  51.  
  52.     /**
  53.     * The result from a select-query
  54.     *
  55.     * @var  object 
  56.     */
  57.     var $result;
  58.  
  59.     /**
  60.     * Flag that tells if you are connected to the database or not
  61.     *
  62.     * @var  boolean 
  63.     */
  64.     var $isConnected;
  65.  
  66.     /**
  67.     * Flag that tells if you the tables are locked or not
  68.     *
  69.     * @var  boolean 
  70.     */
  71.     var $isLocked;
  72.     
  73.     /**
  74.      *This will indicate what querytype the last query was
  75.      *
  76.      * @var    string 
  77.      */
  78.     var $queryType;
  79.  
  80.     /**
  81.      * This is the constructor of this mysql class.
  82.      * It creates a connection to the database, and if possible it sets the database to
  83.      * You can specify if you want to use persistant connections or not.
  84.      *
  85.      * @param     string    The host to the mySQL server
  86.      * @param    string    The username you use to log on to the mySQL server
  87.      * @param    string    The password you use to log on to the mySQL server
  88.      * @param    string    The name of the database you wish to use
  89.      * @param    boolean    TRUE if you want to use persistant connections. Default is TRUE
  90.      * @return    boolean    TRUE when connection was successfull
  91.      * @access    public
  92.      */    
  93.     function __construct($sHost$sUser$sPassword$sDatabase=""$bPersistant=TRUE{
  94.         $conFunc "";
  95.         
  96.         if(!defined("DEBUG")) {
  97.             define("DEBUG"FALSE);
  98.         }
  99.         
  100.         if($this->getConnected()) {
  101.             $this->closeConnection();
  102.         }
  103.         if($this->connection = ($bPersistant mysql_pconnect($sHost$sUser$sPasswordmysql_connect($sHost$sUser$sPassword))) {
  104.             $this->setConnected(TRUE);
  105.             
  106.             if($sDatabase{
  107.                 $this->setDb($sDatabase);
  108.             
  109.             }
  110.             
  111.             return TRUE;
  112.         else {
  113.             $this->setConnected(FALSE);
  114.             return FALSE;
  115.         }
  116.     }
  117.     
  118.     /**
  119.      * This is the destructor of this class. It frees the result of a query,
  120.      * it unlocks all locked tables and close the connection to the database
  121.      * It does not return anything at all, so you will not know if it was sauccessfull
  122.      *
  123.      * @access    public
  124.      */
  125.     function __destruct({
  126.         if($this->result{
  127.             $this->freeResult();
  128.         }
  129.         if($this->getLocked()) {
  130.             $this->unlock();
  131.         }
  132.         if($this->getConnected()) {
  133.             $this->closeConnection();
  134.         }
  135.     }
  136.     
  137.     /**
  138.      * This function frees the result from a query if there is any result.
  139.      *
  140.      * @access    public
  141.      */
  142.     function freeResult({
  143.         if($this->result{
  144.             @mysql_free_result($this->result);
  145.         }
  146.     }
  147.     
  148.     /**
  149.      * This function executes a query to the database.
  150.      * The function does not return the result of the query, you must call the
  151.      * function getQueryResult() to fetch the result
  152.      *
  153.      * @param     string    The query-string to execute
  154.      * @param     bool    Whether to ignore errors if encoutered. Good for catching duplicate errors and not logging them
  155.      * @return    boolean    TRUE if the query was successfull
  156.      * @access    public
  157.      */
  158.     function query($query$IgnoreErrors FALSE{
  159.  
  160.         if(strlen(trim($query)) == 0{
  161.             $this->printError("No query got in function query()");
  162.             return FALSE;
  163.         }
  164.         if(!$this->getConnected()) {
  165.             $this->printError("Not connected in function query()");
  166.             return FALSE;
  167.         }
  168.         
  169.         $queryType substr(trim($query)0strpos($query" "));
  170.         $this->setQueryType($queryType);
  171.  
  172.         $this->result = mysql_query($query$this->connection);
  173.  
  174.         if($this->result || $IgnoreErrors{
  175.             return $this->result;
  176.         }
  177.         $this->printError($this->getMysqlError().'<br>'.$query);
  178.         return FALSE;
  179.     }
  180.  
  181.     function execute($sql){
  182.         return $this->query($sql);
  183.     }
  184.     
  185.     /**
  186.      * Sets the querytype of the last query executed
  187.      * For example it can be SELECT, UPDATE, DELETE etc.
  188.      *
  189.      * @access    private
  190.      */
  191.     function setQueryType($type{
  192.         $this->queryType = strtoupper($type);
  193.     }
  194.     
  195.     /**
  196.      * Returns the querytype
  197.      *
  198.      * @return    string 
  199.      * @access    private
  200.      */
  201.     function getQueryType({
  202.         return $this->queryType;
  203.     }
  204.  
  205.     /**
  206.      * Returns the latest insert_id
  207.      *
  208.      * @return    string 
  209.      * @access    private
  210.      */
  211.     function getInsertId({
  212.         return mysql_insert_id();
  213.     }
  214.     
  215.     /**
  216.      * This function returns number of rows got when executing a query
  217.      *
  218.      * @return    mixed    FALSE if there is no query-result.
  219.      *                     If the queryType is SELECT then it will use the function MYSQL_NUM_ROWS
  220.      *                     Otherwise it uses the MYSQL_AFFECTED_ROWS
  221.      * @access    public
  222.      */
  223.     function getNumRows({
  224.         if($this->result{
  225.             if(DEBUG==TRUE{
  226.                 print("<font style=\"background-color: red\">".$this->getQueryType()."</font><br>");
  227.             }
  228.             return mysql_affected_rows($this->connection);
  229.         }
  230.         return FALSE;
  231.     }
  232.     
  233.     /**
  234.      * The function returns the result from a call to the query() function
  235.      *
  236.      * @return    object 
  237.      * @access    public
  238.      */
  239.     function getQueryResult({
  240.         return $this->result;
  241.     }
  242.     
  243.     /**
  244.      * This function returns the query result as an array for each row in the query result
  245.      *
  246.      * @return    array 
  247.      * @access    public
  248.      */
  249.     function fetchArray($result FALSE{
  250.         if($result{
  251.             return mysql_fetch_array($result);
  252.         else {
  253.             return mysql_fetch_array($this->result);
  254.         }
  255.         return FALSE;
  256.     }
  257.  
  258.     function query_result(){
  259.         if($this->result{
  260.             return mysql_result($this->result,0);
  261.         }
  262.         return FALSE;
  263.     }
  264.     
  265.     /**
  266.      * This function returns the query result as an object for each row in the query result
  267.      *
  268.      * @return    object 
  269.      * @access    public
  270.      */
  271.     function fetchObject({
  272.         if($this->result{
  273.             return mysql_fetch_object($this->result);
  274.         }
  275.         return FALSE;
  276.     }
  277.     
  278.     /**
  279.      * This function returns the query result as an array for each row in the query result
  280.      *
  281.      * @return    array 
  282.      * @access    public
  283.      */
  284.     function fetchRow({
  285.         if($this->result{
  286.             return mysql_fetch_row($this->result);
  287.         }
  288.         return FALSE;
  289.     }
  290.     
  291.     /**
  292.      * This function sets the database
  293.      *
  294.      * @return    boolean    TRUE if the database was set
  295.      * @access    public
  296.      */
  297.     function setDb($sDatabase{
  298.         if(!$this->getConnected()) {
  299.             $this->printError("Not connected in function setDb()");
  300.             return FALSE;
  301.         }
  302.         if($this->selectedDb = mysql_select_db($sDatabase$this->connection)) {
  303.             return TRUE;
  304.         }
  305.         return FALSE;
  306.     }
  307.     
  308.     /**
  309.      * This function returns a flag so you can see if you are connected to the database
  310.      * or not
  311.      *
  312.      * @return    boolean    TRUE when connected to the database
  313.      * @access    public
  314.      */
  315.     function getConnected({
  316.         return $this->isConnected;
  317.     }
  318.  
  319.     /**
  320.      * This function sets the flag so you can see if you are connected to the database
  321.      *
  322.      * @param    $bStatus    The status of the connection. TRUE if you are connected,
  323.      *                         FALSE if you are not
  324.      * @access    public
  325.      */
  326.     function setConnected($bStatus{
  327.         $this->isConnected = $bStatus;
  328.     }
  329.     
  330.     /**
  331.      * The function unlocks tables if there are locked tables and the closes the
  332.      * connection to the database.
  333.      *
  334.      * @access    public
  335.      */
  336.     function closeConnection({
  337.         if($this->getLocked()) {
  338.             $this->unlock();
  339.         }
  340.         
  341.         if($this->getConnected()) {
  342.             mysql_close($this->connection);
  343.             $this->setConnected(FALSE);
  344.         }
  345.     }
  346.     
  347.     /**
  348.      * Unlocks all tables that are locked
  349.      *
  350.      * @access    public
  351.      */
  352.     function unlock({
  353.         if(!$this->getConnected()) {
  354.             $this->setLocked(FALSE);
  355.         }            
  356.         if($this->getLocked()) {
  357.             $this->query("UNLOCK TABLES")
  358.             $this->setLocked(FALSE);
  359.         }
  360.     }
  361.  
  362.     /**
  363.      * This function locks the table(s) that you specify
  364.      * The type of lock must be specified at the end of the string.
  365.      *
  366.      * @param    string    a string containing the table(s) to lock,
  367.      *                     as well as the type of lock to use (READ or WRITE)
  368.      *                     at the end of the string
  369.      * @return    boolean    TRUE if the tables was successfully locked
  370.      * @access    private
  371.      */
  372.     function lock($sCommand{
  373.         if($this->query("LOCK TABLE ".$sCommand)) {
  374.             $this->setLocked(TRUE);
  375.             return TRUE;
  376.         }
  377.         
  378.         $this->setLocked(FALSE);
  379.         return FALSE;
  380.     }
  381.     
  382.     /**
  383.      * This functions sets read lock to specified table(s)
  384.      *
  385.      * @param    string    a string containing the table(s) to read-lock
  386.      * @return    boolean    TRUE on success
  387.      */
  388.     function setReadLock($sTable{
  389.         return $this->lock($sTable." ".LOCKED_FOR_READ);
  390.     }
  391.     
  392.     /**
  393.      * This functions sets write lock to specified table(s)
  394.      *
  395.      * @param    string    a string containing the table(s) to read-lock
  396.      * @return    boolean TRUE on success
  397.      */
  398.     function setWriteLock($sTable{
  399.         return $this->lock($sTable." ".LOCKED_FOR_WRITE);
  400.     }
  401.         
  402.     /**
  403.      * Sets the flag that indicates if there is any tables locked
  404.      *
  405.      * @param    boolean    The flag that will indicate the lock. TRUE if locked
  406.      */
  407.     function setLocked($bStatus{
  408.         $this->isLocked = $bStatus;
  409.     }
  410.     
  411.     /**
  412.      * Returns TRUE if there is any locked tables
  413.      *
  414.      * @return    boolean TRUE if there are locked tables
  415.      */
  416.     function getLocked({
  417.         return $this->isLocked;
  418.     }
  419.  
  420.     /**
  421.      * Returns TRUE if there is any locked tables
  422.      *
  423.      * @return    boolean TRUE if there are locked tables
  424.      */
  425.     function escapeString($string{
  426.         return mysql_real_escape_string($string);
  427.     }
  428.  
  429.     /**
  430.      * Alias for escapeString()
  431.      *
  432.      * @return    boolean TRUE if there are locked tables
  433.      */
  434.     function es($string{
  435.         return $this->escapeString($string);
  436.     }
  437.  
  438.     /**
  439.      * Prints an error to the screen. Can be used to kill the application
  440.      *
  441.      * @param    string    The text to display
  442.      * @param    boolean    TRUE if you want to kill the application. Default is FALSE
  443.      */
  444.     function printError($text$killApp=FALSE{
  445.         global $vars;
  446.         if($text{
  447.             trigger_error($textE_USER_ERROR);
  448.             log_error($text"mysql"__LINE____FILE__);
  449.         }
  450.         if($killApp{
  451.             exit();
  452.         }
  453.     }
  454.     
  455.     /**
  456.      * Returns any mysql-error number
  457.      *
  458.      * @return    mixed    String with the error if there is any error.
  459.      *                     Otherwise it returns FALSE
  460.      */
  461.     function getMysqlErrorNo({
  462.         if(mysql_error()) {
  463.             return mysql_errno();
  464.         }
  465.         return FALSE;
  466.     }
  467.  
  468.     /**
  469.      * Returns any mysql-error
  470.      *
  471.      * @return    mixed    String with the error if there is any error.
  472.      *                     Otherwise it returns FALSE
  473.      */
  474.     function getMysqlError({
  475.         if(mysql_error()) {
  476.             return mysql_error();
  477.         }
  478.         return FALSE;
  479.     }
  480. }
  481. ?>

Documentation generated on Thu, 01 Jan 2009 14:04:21 -0800 by phpDocumentor 1.4.2