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/mysqli.php

Documentation is available at mysqli.php

  1. <?php
  2. /**
  3. @version        $Id: mysqli.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.  * MySQLi database driver
  20.  *
  21.  * @package        Joomla.Framework
  22.  * @subpackage    Database
  23.  * @since        1.0
  24.  */
  25. class JDatabaseMySQLi extends JDatabase
  26. {
  27.     /**
  28.      *  The database driver name
  29.      *
  30.      * @var string 
  31.      */
  32.     var $name            = 'mysqli';
  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.         // Unlike mysql_connect(), mysqli_connect() takes the port and socket
  66.         // as separate arguments. Therefore, we have to extract them from the
  67.         // host string.
  68.         $port    NULL;
  69.         $socket    NULL;
  70.         $targetSlot substrstrstr$host":" ));
  71.         if (!empty$targetSlot )) {
  72.             // Get the port number or socket name
  73.             if (is_numeric$targetSlot ))
  74.                 $port    $targetSlot;
  75.             else
  76.                 $socket    $targetSlot;
  77.  
  78.             // Extract the host name only
  79.             $host substr$host0strlen$host (strlen$targetSlot 1) );
  80.             // This will take care of the following notation: ":3306"
  81.             if($host == '')
  82.                 $host 'localhost';
  83.         }
  84.  
  85.         // perform a number of fatality checks, then return gracefully
  86.         if (!function_exists'mysqli_connect' )) {
  87.             $this->_errorNum = 1;
  88.             $this->_errorMsg = 'The MySQL adapter "mysqli" is not available.';
  89.             return;
  90.         }
  91.  
  92.         // connect to the server
  93.         if (!($this->_resource = @mysqli_connect($host$user$passwordNULL$port$socket))) {
  94.             $this->_errorNum = 2;
  95.             $this->_errorMsg = 'Could not connect to MySQL';
  96.             return;
  97.         }
  98.  
  99.         // finalize initialization
  100.         parent::__construct($options);
  101.  
  102.         // select the database
  103.         if $select {
  104.             $this->select($database);
  105.         }
  106.     }
  107.  
  108.     /**
  109.      * Database object destructor
  110.      *
  111.      * @return boolean 
  112.      * @since 1.5
  113.      */
  114.     function __destruct()
  115.     {
  116.         $return false;
  117.         if (is_resource($this->_resource)) {
  118.             $return mysqli_close($this->_resource);
  119.         }
  120.         return $return;
  121.     }
  122.  
  123.     /**
  124.      * Test to see if the MySQLi connector is available
  125.      *
  126.      * @static
  127.      * @access public
  128.      * @return boolean  True on success, false otherwise.
  129.      */
  130.     function test()
  131.     {
  132.         return (function_exists'mysqli_connect' ));
  133.     }
  134.  
  135.     /**
  136.      * Determines if the connection to the server is active.
  137.      *
  138.      * @access    public
  139.      * @return    boolean 
  140.      * @since    1.5
  141.      */
  142.     function connected()
  143.     {
  144.         return $this->_resource->ping();
  145.     }
  146.  
  147.     /**
  148.      * Select a database for use
  149.      *
  150.      * @access    public
  151.      * @param    string $database 
  152.      * @return    boolean True if the database has been successfully selected
  153.      * @since    1.5
  154.      */
  155.     function select($database)
  156.     {
  157.         if $database )
  158.         {
  159.             return false;
  160.         }
  161.  
  162.         if !mysqli_select_db($this->_resource$database)) {
  163.             $this->_errorNum = 3;
  164.             $this->_errorMsg = 'Could not connect to database';
  165.             return false;
  166.         }
  167.  
  168.         // if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems
  169.         if strpos$this->getVersion()'5' === {
  170.             $this->setQuery"SET sql_mode = 'MYSQL40'" );
  171.             $this->query();
  172.         }
  173.  
  174.         return true;
  175.     }
  176.  
  177.     /**
  178.      * Determines UTF support
  179.      *
  180.      * @access public
  181.      * @return boolean True - UTF is supported
  182.      */
  183.     function hasUTF()
  184.     {
  185.         $verParts explode'.'$this->getVersion() );
  186.         return ($verParts[0== || ($verParts[0== && $verParts[1== && (int)$verParts[2>= 2));
  187.     }
  188.  
  189.     /**
  190.      * Custom settings for UTF support
  191.      *
  192.      * @access public
  193.      */
  194.     function setUTF()
  195.     {
  196.         mysqli_query$this->_resource"SET NAMES 'utf8'" );
  197.     }
  198.  
  199.     /**
  200.      * Get a database escaped string
  201.      *
  202.      * @param    string    The string to be escaped
  203.      * @param    boolean    Optional parameter to provide extra escaping
  204.      * @return    string 
  205.      * @access    public
  206.      * @abstract
  207.      */
  208.     function getEscaped$text$extra false )
  209.     {
  210.         $result mysqli_real_escape_string$this->_resource$text );
  211.         if ($extra{
  212.             $result addcslashes$result'%_' );
  213.         }
  214.         return $result;
  215.     }
  216.     /**
  217.     * Execute the query
  218.     *
  219.     * @access public
  220.     * @return mixed A database resource if successful, FALSE if not.
  221.     */
  222.     function query()
  223.     {
  224.         if (!is_object($this->_resource)) {
  225.             return false;
  226.         }
  227.  
  228.         if ($this->_limit > || $this->_offset > 0{
  229.             $this->_sql .= ' LIMIT '.$this->_offset.', '.$this->_limit;
  230.         }
  231.         if ($this->_debug{
  232.             $this->_ticker++;
  233.             $this->_log[$this->_sql;
  234.         }
  235.         $this->_errorNum = 0;
  236.         $this->_errorMsg = '';
  237.         $this->_cursor = mysqli_query$this->_resource$this->_sql );
  238.  
  239.         if (!$this->_cursor)
  240.         {
  241.             $this->_errorNum = mysqli_errno$this->_resource );
  242.             $this->_errorMsg = mysqli_error$this->_resource )." SQL=$this->_sql";
  243.  
  244.             if ($this->_debug{
  245.                 JError::raiseError(500'JDatabaseMySQL::query: '.$this->_errorNum.' - '.$this->_errorMsg );
  246.             }
  247.             return false;
  248.         }
  249.         return $this->_cursor;
  250.     }
  251.  
  252.     /**
  253.      * Description
  254.      *
  255.      * @access public
  256.      * @return int The number of affected rows in the previous operation
  257.      * @since 1.0.5
  258.      */
  259.     function getAffectedRows()
  260.     {
  261.         return mysqli_affected_rows$this->_resource );
  262.     }
  263.  
  264.     /**
  265.     * Execute a batch query
  266.     *
  267.     * @access public
  268.     * @return mixed A database resource if successful, FALSE if not.
  269.     */
  270.     function queryBatch$abort_on_error=true$p_transaction_safe false)
  271.     {
  272.         $this->_errorNum = 0;
  273.         $this->_errorMsg = '';
  274.         if ($p_transaction_safe{
  275.             $si $this->getVersion();
  276.             preg_match_all"/(\d+)\.(\d+)\.(\d+)/i"$si$m );
  277.             if ($m[1>= 4{
  278.                 $this->_sql = 'START TRANSACTION;' $this->_sql . '; COMMIT;';
  279.             else if ($m[2>= 23 && $m[3>= 19{
  280.                 $this->_sql = 'BEGIN WORK;' $this->_sql . '; COMMIT;';
  281.             else if ($m[2>= 23 && $m[3>= 17{
  282.                 $this->_sql = 'BEGIN;' $this->_sql . '; COMMIT;';
  283.             }
  284.         }
  285.         $query_split preg_split ("/[;]+/"$this->_sql);
  286.         $error 0;
  287.         foreach ($query_split as $command_line{
  288.             $command_line trim$command_line );
  289.             if ($command_line != ''{
  290.                 $this->_cursor = mysqli_query$this->_resource$command_line );
  291.                 if (!$this->_cursor{
  292.                     $error 1;
  293.                     $this->_errorNum .= mysqli_errno$this->_resource ' ';
  294.                     $this->_errorMsg .= mysqli_error$this->_resource )." SQL=$command_line <br />";
  295.                     if ($abort_on_error{
  296.                         return $this->_cursor;
  297.                     }
  298.                 }
  299.             }
  300.         }
  301.         return $error false true;
  302.     }
  303.  
  304.     /**
  305.      * Diagnostic function
  306.      *
  307.      * @access public
  308.      * @return    string 
  309.      */
  310.     function explain()
  311.     {
  312.         $temp $this->_sql;
  313.         $this->_sql = "EXPLAIN $this->_sql";
  314.  
  315.         if (!($cur $this->query())) {
  316.             return null;
  317.         }
  318.         $first true;
  319.  
  320.         $buffer '<table id="explain-sql">';
  321.         $buffer .= '<thead><tr><td colspan="99">'.$this->getQuery().'</td></tr>';
  322.         while ($row mysqli_fetch_assoc$cur )) {
  323.             if ($first{
  324.                 $buffer .= '<tr>';
  325.                 foreach ($row as $k=>$v{
  326.                     $buffer .= '<th>'.$k.'</th>';
  327.                 }
  328.                 $buffer .= '</tr>';
  329.                 $first false;
  330.             }
  331.             $buffer .= '</thead><tbody><tr>';
  332.             foreach ($row as $k=>$v{
  333.                 $buffer .= '<td>'.$v.'</td>';
  334.             }
  335.             $buffer .= '</tr>';
  336.         }
  337.         $buffer .= '</tbody></table>';
  338.         mysqli_free_result$cur );
  339.  
  340.         $this->_sql = $temp;
  341.  
  342.         return $buffer;
  343.     }
  344.  
  345.     /**
  346.      * Description
  347.      *
  348.      * @access public
  349.      * @return int The number of rows returned from the most recent query.
  350.      */
  351.     function getNumRows$cur=null )
  352.     {
  353.         return mysqli_num_rows$cur $cur $this->_cursor );
  354.     }
  355.  
  356.     /**
  357.     * This method loads the first field of the first row returned by the query.
  358.     *
  359.     * @access public
  360.     * @return The value returned in the query or null if the query failed.
  361.     */
  362.     function loadResult()
  363.     {
  364.         if (!($cur $this->query())) {
  365.             return null;
  366.         }
  367.         $ret null;
  368.         if ($row mysqli_fetch_row$cur )) {
  369.             $ret $row[0];
  370.         }
  371.         mysqli_free_result$cur );
  372.         return $ret;
  373.     }
  374.  
  375.     /**
  376.     * Load an array of single field results into an array
  377.     *
  378.     * @access public
  379.     */
  380.     function loadResultArray($numinarray 0)
  381.     {
  382.         if (!($cur $this->query())) {
  383.             return null;
  384.         }
  385.         $array array();
  386.         while ($row mysqli_fetch_row$cur )) {
  387.             $array[$row[$numinarray];
  388.         }
  389.         mysqli_free_result$cur );
  390.         return $array;
  391.     }
  392.  
  393.     /**
  394.     * Fetch a result row as an associative array
  395.     *
  396.     * @access public
  397.     * @return array 
  398.     */
  399.     function loadAssoc()
  400.     {
  401.         if (!($cur $this->query())) {
  402.             return null;
  403.         }
  404.         $ret null;
  405.         if ($array mysqli_fetch_assoc$cur )) {
  406.             $ret $array;
  407.         }
  408.         mysqli_free_result$cur );
  409.         return $ret;
  410.     }
  411.  
  412.     /**
  413.     * Load a assoc list of database rows
  414.     *
  415.     * @access public
  416.     * @param string The field name of a primary key
  417.     * @return array If <var>key</var> is empty as sequential list of returned records.
  418.     */
  419.     function loadAssocList$key='' )
  420.     {
  421.         if (!($cur $this->query())) {
  422.             return null;
  423.         }
  424.         $array array();
  425.         while ($row mysqli_fetch_assoc$cur )) {
  426.             if ($key{
  427.                 $array[$row[$key]] $row;
  428.             else {
  429.                 $array[$row;
  430.             }
  431.         }
  432.         mysqli_free_result$cur );
  433.         return $array;
  434.     }
  435.  
  436.     /**
  437.     * This global function loads the first row of a query into an object
  438.     *
  439.     * @access public
  440.     * @return object 
  441.     */
  442.     function loadObject)
  443.     {
  444.         if (!($cur $this->query())) {
  445.             return null;
  446.         }
  447.         $ret null;
  448.         if ($object mysqli_fetch_object$cur )) {
  449.             $ret $object;
  450.         }
  451.         mysqli_free_result$cur );
  452.         return $ret;
  453.     }
  454.  
  455.     /**
  456.     * Load a list of database objects
  457.     *
  458.     * If <var>key</var> is not empty then the returned array is indexed by the value
  459.     * the database key.  Returns <var>null</var> if the query fails.
  460.     *
  461.     * @access public
  462.     * @param string The field name of a primary key
  463.     * @return array If <var>key</var> is empty as sequential list of returned records.
  464.     */
  465.     function loadObjectList$key='' )
  466.     {
  467.         if (!($cur $this->query())) {
  468.             return null;
  469.         }
  470.         $array array();
  471.         while ($row mysqli_fetch_object