Location: PHPKode > projects > Moc10 PHP Library > library/Moc10/Db/MySQLi.php
<?php
/**
 * Moc10 Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.TXT.
 * It is also available through the world-wide-web at this URL:
 * http://www.moc10phplibrary.com/LICENSE.TXT
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @category   Moc10
 * @package    Moc10_Db
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 */

/**
 * Moc10_Db_MySQLi
 *
 * @category   Moc10
 * @package    Moc10_Db
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 * @version    1.9.7
 */

class Moc10_Db_MySQLi
{

    /**
     * Database results
     * @var resource
     */
    public $result;

    /**
     * Default database connection
     * @var resource
     */
    public $connection;

    /**
     * Database tables
     * @var array
     */
    protected $_tables = array();

    /**
     * Language object
     * @var Moc10_Language
     */
    protected $_lang = null;

    /**
     * Constructor
     *
     * Instantiate the MySQL database connection object.
     *
     * @param  string $dbase
     * @param  string $host
     * @param  string $user
     * @param  string $pass
     * @throws Exception
     * @return void
     */
    public function __construct($dbase, $host, $user, $pass)
    {

        $this->_lang = new Moc10_Language();

        if (is_null($dbase) && is_null($host) && is_null($user) && is_null($pass)) {
            throw new Exception($this->_lang->__('Error: The proper database credentials were not passed.'));
        } else {
            $this->connection = new mysqli($host, $user, $pass, $dbase);

            if ($this->connection->connect_error != '') {
                throw new Exception($this->_lang->__('Error: Could not connect to database. Connection Error #%1: %2.', array($this->connection->connect_errno, $this->connection->connect_error)));
            }
        }

    }

    /**
     * Throw an exception upon a database error.
     *
     * @throws Exception
     * @return void
     */
    public function showError()
    {

        throw new Exception($this->_lang->__('Error:') . ' ' . $this->connection->errno . ' => ' . $this->connection->error . '.');

    }

    /**
     * Execute the SQL query and create a result resource, or display the SQL error.
     *
     * @param  string $sql
     * @return void
     */
    public function query($sql)
    {

        if (!($this->result = $this->connection->query($sql))) {
            $this->showError();
        }

    }

    /**
     * Return the results array from the results resource.
     *
     * @throws Exception
     * @return array
     */
    public function fetch()
    {

        if (!isset($this->result)) {
            throw new Exception($this->_lang->__('Error: The database result resource is not currently set.'));
        } else {
            return $this->result->fetch_array(MYSQLI_ASSOC);
        }

    }

    /**
     * Return the escapes string value.
     *
     * @param  string $value
     * @return string
     */
    public function escape($value)
    {

        return $this->connection->real_escape_string($value);

    }

    /**
     * Return the auto-increment ID of the last query.
     *
     * @return int
     */
    public function lastId()
    {

        return $this->connection->insert_id;

    }

    /**
     * Return the number of rows in the result.
     *
     * @throws Exception
     * @return int
     */
    public function numRows()
    {

        if (!isset($this->result)) {
            throw new Exception($this->_lang->__('Error: The database result resource is not currently set.'));
        } else {
            return $this->result->num_rows;
        }

    }

    /**
     * Return the number of fields in the result.
     *
     * @throws Exception
     * @return int
     */
    public function numFields()
    {

        if (!isset($this->result)) {
            throw new Exception($this->_lang->__('Error: The database result resource is not currently set.'));
        } else {
            return $this->connection->field_count;
        }

    }

    /**
     * Get tables of the database.
     *
     * @return array
     */
    public function getTables()
    {

        $this->_tables = array();

        $this->query('SHOW TABLES');
        while (($row = $this->fetch()) != false) {
            foreach($row as $value) {
                $this->_tables[] = $value;
            }
        }

        return $this->_tables;

    }

    /**
     * Return the MySQL server version.
     *
     * @return string
     */
    public function version()
    {

        return 'MySQL ' . $this->connection->server_info;

    }

    /**
     * Close the DB connection.
     *
     * @return void
     */
    public function __destruct()
    {

        $this->connection->close();

    }

}
Return current item: Moc10 PHP Library