Location: PHPKode > projects > Moc10 PHP Library > library/Moc10/Db/MySQL.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_MySQL
 *
 * @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_MySQL
{

    /**
     * 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 = mysql_connect($host, $user, $pass);

            // Select the DB to use, or display the SQL error.
            if (!(mysql_select_db($dbase, $this->connection))) {
                $this->showError();
            }
        }

    }

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

        throw new Exception($this->_lang->__('Error:') . ' ' . mysql_errno() . ' => ' . mysql_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 = mysql_query($sql, $this->connection))) {
            $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 mysql_fetch_array($this->result, MYSQL_ASSOC);
        }

    }

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

        return mysql_real_escape_string($value);

    }

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

        return mysql_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 mysql_num_rows($this->result);
        }

    }

    /**
     * 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 mysql_num_fields($this->result);
        }

    }

    /**
     * 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 ' . mysql_get_server_info($this->connection);

    }

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

        mysql_close($this->connection);

    }

}
Return current item: Moc10 PHP Library