Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/database/database/mysql.php

Documentation is available at mysql.php

  1. <?php
  2. /**
  3. @version        $Id: mysql.php 9870 2008-01-05 10:56:58Z eddieajau $
  4. @package        Joomla.Framework
  5. @subpackage    Database
  6. @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. @license        GNU/GPL, see LICENSE.php
  8. *  Joomla! is free software. This version may have been modified pursuant
  9. *  to the GNU General Public License, and as distributed it includes or
  10. *  is derivative of works licensed under the GNU General Public License or
  11. *  other free or open source software licenses.
  12. *  See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * MySQL database driver
  20.  *
  21.  * @package        Joomla.Framework
  22.  * @subpackage    Database
  23.  * @since        1.0
  24.  */
  25. class JDatabaseMySQL extends JDatabase
  26. {
  27.     /**
  28.      * The database driver name
  29.      *
  30.      * @var string 
  31.      */
  32.     var $name            = 'mysql';
  33.  
  34.     /**
  35.      *  The null/zero date string
  36.      *
  37.      * @var string 
  38.      */
  39.     var $_nullDate        = '0000-00-00 00:00:00';
  40.  
  41.     /**
  42.      * Quote for named objects
  43.      *
  44.      * @var string 
  45.      */
  46.     var $_nameQuote        = '`';
  47.  
  48.     /**
  49.     * Database object constructor
  50.     *
  51.     * @access    public
  52.     * @param    array    List of options used to configure the connection
  53.     * @since    1.5
  54.     * @see        JDatabase
  55.     */
  56.     function __construct$options )
  57.     {
  58.         $host        array_key_exists('host'$options)    $options['host']        'localhost';
  59.         $user        array_key_exists('user'$options)    $options['user']        '';
  60.         $password    array_key_exists('password',$options)    $options['password']    '';
  61.         $database    array_key_exists('database',$options)    $options['database']    '';
  62.         $prefix        array_key_exists('prefix'$options)    $options['prefix']    'jos_';
  63.         $select        array_key_exists('select'$options)    $options['select']    true;
  64.  
  65.         // perform a number of fatality checks, then return gracefully
  66.         if (!function_exists'mysql_connect' )) {
  67.             $this->_errorNum = 1;
  68.             $this->_errorMsg = 'The MySQL adapter "mysql" is not available.';
  69.             return;
  70.         }
  71.  
  72.         // connect to the server
  73.         if (!($this->_resource = @mysql_connect$host$user$passwordtrue ))) {
  74.             $this->_errorNum = 2;
  75.             $this->_errorMsg = 'Could not connect to MySQL';
  76.             return;
  77.         }
  78.  
  79.         // finalize initialization
  80.         parent::__construct($options);
  81.  
  82.         // select the database
  83.         if $select {
  84.             $this->select($database);
  85.         }
  86.     }
  87.  
  88.     /**
  89.      * Database object destructor
  90.      *
  91.      * @return boolean 
  92.      * @since 1.5
  93.      */
  94.     function __destruct()
  95.     {
  96.         $return false;
  97.         if (is_resource($this->_resource)) {
  98.             $return mysql_close($this->_resource);
  99.         }
  100.         return $return;
  101.     }
  102.  
  103.     /**
  104.      * Test to see if the MySQL connector is available
  105.      *
  106.      * @static
  107.      * @access public
  108.      * @return boolean  True on success, false otherwise.
  109.      */
  110.     function test()
  111.     {
  112.         return (function_exists'mysql_connect' ));
  113.     }
  114.  
  115.     /**
  116.      * Determines if the connection to the server is active.
  117.      *
  118.      * @access    public
  119.      * @return    boolean 
  120.      * @since    1.5
  121.      */
  122.     function connected()
  123.     {
  124.         if(is_resource($this->_resource)) {
  125.             return mysql_ping($this->_resource);
  126.         }
  127.         return false;
  128.     }
  129.  
  130.     /**
  131.      * Select a database for use
  132.      *
  133.      * @access    public
  134.      * @param    string $database 
  135.      * @return    boolean True if the database has been successfully selected
  136.      * @since    1.5
  137.      */
  138.     function select($database)
  139.     {
  140.         if $database )
  141.         {
  142.             return false;
  143.         }
  144.  
  145.         if !mysql_select_db$database$this->_resource )) {
  146.             $this->_errorNum = 3;
  147.             $this->_errorMsg = 'Could not connect to database';
  148.             return false;
  149.         }
  150.  
  151.         // if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems
  152.         if strpos$this->getVersion()'5' === {
  153.             $this->setQuery"SET sql_mode = 'MYSQL40'" );
  154.             $this->query();
  155.         }
  156.  
  157.         return true;
  158.     }
  159.  
  160.     /**
  161.      * Determines UTF support
  162.      *
  163.      * @access    public
  164.      * @return boolean True - UTF is supported
  165.      */
  166.     function hasUTF()
  167.     {
  168.         $verParts explode'.'$this->getVersion() );
  169.         return ($verParts[0== || ($verParts[0== && $verParts[1== && (int)$verParts[2>= 2));
  170.     }
  171.  
  172.     /**
  173.      * Custom settings for UTF support
  174.      *
  175.      * @access    public
  176.      */
  177.     function setUTF()
  178.     {
  179.         mysql_query"SET NAMES 'utf8'"$this->_resource );
  180.     }
  181.  
  182.     /**
  183.      * Get a database escaped string
  184.      *
  185.      * @param    string    The string to be escaped
  186.      * @param    boolean    Optional parameter to provide extra escaping
  187.      * @return    string 
  188.      * @access    public
  189.      * @abstract
  190.      */
  191.     function getEscaped$text$extra false )
  192.     {
  193.         $result mysql_real_escape_string$text$this->_resource );
  194.         if ($extra{
  195.             $result addcslashes$result'%_' );
  196.         }
  197.         return $result;
  198.     }
  199.  
  200.     /**
  201.      * Execute the query
  202.      *
  203.      * @access    public
  204.      * @return mixed A database resource if successful, FALSE if not.
  205.      */
  206.     function query()
  207.     {
  208.         if (!is_resource($this->_resource)) {
  209.             return false;
  210.         }
  211.  
  212.         if ($this->_limit > || $this->_offset > 0{
  213.             $this->_sql .= ' LIMIT '.$this->_offset.', '.$this->_limit;
  214.         }
  215.         if ($this->_debug{
  216.             $this->_ticker++;
  217.             $this->_log[$this->_sql;
  218.         }
  219.         $this->_errorNum = 0;
  220.         $this->_errorMsg = '';
  221.         $this->_cursor = mysql_query$this->_sql$this->_resource );
  222.  
  223.         if (!$this->_cursor)
  224.         {
  225.             $this->_errorNum = mysql_errno$this->_resource );
  226.             $this->_errorMsg = mysql_error$this->_resource )." SQL=$this->_sql";
  227.  
  228.             if ($this->_debug{
  229.                 JError::raiseError(500'JDatabaseMySQL::query: '.$this->_errorNum.' - '.$this->_errorMsg );
  230.             }
  231.             return false;
  232.         }
  233.         return $this->_cursor;
  234.     }
  235.  
  236.     /**
  237.      * Description
  238.      *
  239.      * @access    public
  240.      * @return int The number of affected rows in the previous operation
  241.      * @since 1.0.5
  242.      */
  243.     function getAffectedRows()
  244.     {
  245.         return mysql_affected_rows$this->_resource );
  246.     }
  247.  
  248.     /**
  249.      * Execute a batch query
  250.      *
  251.      * @access    public
  252.      * @return mixed A database resource if successful, FALSE if not.
  253.      */
  254.     function queryBatch$abort_on_error=true$p_transaction_safe false)
  255.     {
  256.         $this->_errorNum = 0;
  257.         $this->_errorMsg = '';
  258.         if ($p_transaction_safe{
  259.             $si $this->getVersion();
  260.             preg_match_all"/(\d+)\.(\d+)\.(\d+)/i"$si$m );
  261.             if ($m[1>= 4{
  262.                 $this->_sql = 'START TRANSACTION;' $this->_sql . '; COMMIT;';
  263.             else if ($m[2>= 23 && $m[3>= 19{
  264.                 $this->_sql = 'BEGIN WORK;' $this->_sql . '; COMMIT;';
  265.             else if ($m[2>= 23 && $m[3>= 17{
  266.                 $this->_sql = 'BEGIN;' $this->_sql . '; COMMIT;';
  267.             }
  268.         }
  269.         $query_split preg_split ("/[;]+/"$this->_sql);
  270.         $error 0;
  271.         foreach ($query_split as $command_line{
  272.             $command_line trim$command_line );
  273.             if ($command_line != ''{
  274.                 $this->_cursor = mysql_query$command_line$this->_resource );
  275.                 if (!$this->_cursor{
  276.                     $error 1;
  277.                     $this->_errorNum .= mysql_errno$this->_resource ' ';
  278.                     $this->_errorMsg .= mysql_error$this->_resource )." SQL=$command_line <br />";
  279.                     if ($abort_on_error{
  280.                         return $this->_cursor;
  281.                     }
  282.                 }
  283.             }
  284.         }
  285.         return $error false true;
  286.     }
  287.  
  288.     /**
  289.      * Diagnostic function
  290.      *
  291.      * @access    public
  292.      * @return    string 
  293.      */
  294.     function explain()
  295.     {
  296.         $temp $this->_sql;
  297.         $this->_sql = "EXPLAIN $this->_sql";
  298.  
  299.         if (!($cur $this->query())) {
  300.             return null;
  301.         }
  302.         $first true;
  303.  
  304.         $buffer '<table id="explain-sql">';
  305.         $buffer .= '<thead><tr><td colspan="99">'.$this->getQuery().'</td></tr>';
  306.         while ($row mysql_fetch_assoc$cur )) {
  307.             if ($first{
  308.                 $buffer .= '<tr>';
  309.                 foreach ($row as $k=>$v{
  310.                     $buffer .= '<th>'.$k.'</th>';
  311.                 }
  312.                 $buffer .= '</tr>';
  313.                 $first false;
  314.             }
  315.             $buffer .= '</thead><tbody><tr>';
  316.             foreach ($row as $k=>$v{
  317.                 $buffer .= '<td>'.$v.'</td>';
  318.             }
  319.             $buffer .= '</tr>';
  320.         }
  321.         $buffer .= '</tbody></table>';
  322.         mysql_free_result$cur );
  323.  
  324.         $this->_sql = $temp;
  325.  
  326.         return $buffer;
  327.     }
  328.  
  329.     /**
  330.      * Description
  331.      *
  332.      * @access    public
  333.      * @return int The number of rows returned from the most recent query.
  334.      */
  335.     function getNumRows$cur=null )
  336.     {
  337.         return mysql_num_rows$cur $cur $this->_cursor );
  338.     }
  339.  
  340.     /**
  341.      * This method loads the first field of the first row returned by the query.
  342.      *
  343.      * @access    public
  344.      * @return The value returned in the query or null if the query failed.
  345.      */
  346.     function loadResult()
  347.     {
  348.         if (!($cur $this->query())) {
  349.             return null;
  350.         }
  351.         $ret null;
  352.         if ($row mysql_fetch_row$cur )) {
  353.             $ret $row[0];
  354.         }
  355.         mysql_free_result$cur );
  356.         return $ret;
  357.     }
  358.  
  359.     /**
  360.      * Load an array of single field results into an array
  361.      *
  362.      * @access    public
  363.      */
  364.     function loadResultArray($numinarray 0)
  365.     {
  366.         if (!($cur $this->query())) {
  367.             return null;
  368.         }
  369.         $array array();
  370.         while ($row mysql_fetch_row$cur )) {
  371.             $array[$row[$numinarray];
  372.         }
  373.         mysql_free_result$cur );
  374.         return $array;
  375.     }
  376.  
  377.     /**
  378.     * Fetch a result row as an associative array
  379.     *
  380.     * @access    public
  381.     * @return array 
  382.     */
  383.     function loadAssoc()
  384.     {
  385.         if (!($cur $this->query())) {
  386.             return null;
  387.         }
  388.         $ret null;
  389.         if ($array mysql_fetch_assoc$cur )) {
  390.             $ret $array;
  391.         }
  392.         mysql_free_result$cur );
  393.         return $ret;
  394.     }
  395.  
  396.     /**
  397.     * Load a assoc list of database rows
  398.     *
  399.     * @access    public
  400.     * @param string The field name of a primary key
  401.     * @return array If <var>key</var> is empty as sequential list of returned records.
  402.     */
  403.     function loadAssocList$key='' )
  404.     {
  405.         if (!($cur $this->query())) {
  406.             return null;
  407.         }
  408.         $array array();
  409.         while ($row mysql_fetch_assoc$cur )) {
  410.             if ($key{
  411.                 $array[$row[$key]] $row;
  412.             else {
  413.                 $array[$row;
  414.             }
  415.         }
  416.         mysql_free_result$cur );
  417.         return $array;
  418.     }
  419.  
  420.     /**
  421.     * This global function loads the first row of a query into an object
  422.     *
  423.     * @access    public
  424.     * @return     object 
  425.     */
  426.     function loadObject)
  427.     {
  428.         if (!($cur $this->query())) {
  429.             return null;
  430.         }
  431.         $ret null;
  432.         if ($object mysql_fetch_object$cur )) {
  433.             $ret $object;
  434.         }
  435.         mysql_free_result$cur );
  436.         return $ret;
  437.     }
  438.  
  439.     /**
  440.     * Load a list of database objects
  441.     *
  442.     * If <var>key</var> is not empty then the returned array is indexed by the value
  443.     * the database key.  Returns <var>null</var> if the query fails.
  444.     *
  445.     * @access    public
  446.     * @param string The field name of a primary key
  447.     * @return array If <var>key</var> is empty as sequential list of returned records.
  448.     */
  449.     function loadObjectList$key='' )
  450.     {
  451.         if (!($cur $this->query())) {
  452.             return null;
  453.         }
  454.         $array array();
  455.         while ($row mysql_fetch_object$cur )) {
  456.             if ($key{
  457.                 $array[$row->$key$row;
  458.             else {
  459.                 $array[$row;
  460.             }
  461.         }
  462.         mysql_free_result$cur );
  463.         return $array;
  464.     }
  465.  
  466.     /**
  467.      * Description
  468.      *
  469.      * @access    public
  470.      * @return The first row of the query.
  471.      */
  472.     function loadRow()
  473.     {
  474.         if (!($cur $this->query())) {
  475.             return null;