Файл: wboard/source/system/classes/db.php
Строк: 78
<?php
/**
 * Wboard
 * Provides access to the database
 * @author Screamer
 * @copyright 2013
 */
class Db extends mysqli
{
    /**
     * @var (array)
     * Contains all errors which were occured
     */
    protected $_debug_messages = array();
    /**
     * Construct
     * Get connection parameters. Connect to mysql server. Setup connection charset
     * Description of connection parameters:
     * @param (string) $host     hostname
     * @param (string) $user     username
     * @param (string) $password password
     * @param (string) $db       database
     * @param (string) $charset  charset
     * @throws (Exception) Unable to connect to MySQL server
     * @return (void)
     */
    public function __construct($host = 'localhost', $user = 'root', $password = '', $db = 'localhost', $charset = 'utf8')
    {
        if (parent::__construct($host, $user, $password, $db) === FALSE) {
            throw new Exception('Unable to connect to MySQL server. Check your configuration file.');
        }
        // Setup charset
        $this->query("SET NAMES " . $charset);
    }
    /**
     * Analog of mysql_result()
     * @param  (string) $statement Query
     * @return (int)
     */
    public function result($statement)
    {
        $result = $this->query($statement);
        $data = $result->fetch_row();
        $result->free();
        return intval($data[0]);
    }
    /**
     * Run Query
     * @param (string) $statement SQL statement
     * @param (int) $type Result Mode MYSQLI_USE_RESULT|MYSQLI_STORE_RESULT
     * @return (mysqli_result)
     */
    public function query($statement, $type = MYSQLI_USE_RESULT)
    {
        $result = parent::query($statement, $type);
        if (!empty($this->error)) {
            $this->_debug_messages[] = 'Error: ' . $this->errno . ' ' . $this->error . '<br />' . 'Statement: ' . $statement;
        }
        return $result;
    }
    /**
     * Close connection
     * @throws (Exception) Display errors if exists
     * @return (void)
     */
    public function close()
    {
        parent::close();
        try {
            if (!empty($this->_debug_messages)) {
                throw new Exception('<pre>DB ERROR<br />' . implode('<br />--<br />', $this->_debug_messages) . '</pre>');
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}