<?php

if (!defined('IN_GEN')) {
    die('Hacking attempt');
}

class mysql {

    private $connect_id;

    private $num_queries;

    private $perfix;

    public function __construct($dbhost, $dbuser, $dbpw, $dbname, $perfix, $charset = 'utf8', $pconnect = false) {
        $this->_connect($dbhost, $dbuser, $dbpw, $charset);
        $this->query("SET NAMES utf8");
        $this->perfix = $perfix;
        $this->_select_db($dbname);
    }

    public function __destruct() {
        if(is_resource($this->connect_id)) {
            return $this->close($this->connect_id);
        }
        return false;
    }

    public function get_table($table_name) {
        $table_name = $this->perfix . substr($table_name, strpos($table_name, '_') + 1);
        return strtolower($table_name);
    }

    public function _connect($dbhost, $dbuser, $dbpw, $charset, $pconnect = false) {
        $func = ($pconnect === true) ? 'mysql_pconnect' : 'mysql_connect';
        if(!$this->connect_id = @$func($dbhost, $dbuser, $dbpw, $charset, true)) {
            $this->halt('Can not connect to MySQL server');
        }
    }

    public function _select_db($dbname) {
        return mysql_select_db($dbname, $this->connect_id);
    }

    public function query($sql, $type='',$failure_return='0') {

        $func = $type && $type == 'UNBUFFERED' ? 'mysql_unbuffered_query' : 'mysql_query';
        if(!$query = @$func($sql, $this->connect_id)) {
            if ($failure_return == '0') {
                $this->halt('MySQL query failure!', $sql);
            }
            else if ($failure_return == '1') {
                return false;
            }

        }
        ++$this->num_queries;
        return $query;
    }

    public function result($sql) {

        $query = $this->query($sql);
        $result = array();
        while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
            $result[] = $row;
        }
        return $result;
    }

    // 获取查询的结果
    public function fetch_array($query) {
        $result = array();
        while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
            $result[] = $row;
        }
        return $result;
    }

    // 取得前一次操作影响的行数
    public function affected_rows() {
        return mysql_affected_rows($this->connect_id);
    }

    // 取得结果集中的行数
    public function num_rows($query) {
        return mysql_num_rows($query);
    }

    // 取得结果集中字段的数量
    public function num_fields($query) {
        return mysql_num_fields($query);
    }

    // 释放查询结果
    public function free_result($query) {
        return mysql_free_result($query);
    }

    // 最后一次插入数据的ID号
    public function insert_id() {
        return ($id = mysql_insert_id($this->connect_id)) >= 0 ? $id : $this->result($this->_query("SELECT last_insert_id()"), 0);
    }

    // 取出一行数据
    public function fetch_row($query) {
        $query = mysql_fetch_row($query);
        return $query;
    }

    public function get_num_queries() {
        return $this->num_queries;
    }

    // 取得字段信息
    public function fetch_field($query) {
        $arr = array();
        $i = 0;
        while ($i < mysql_num_fields($query)) {
            $arr[] = mysql_field_name($query, $i);
            $i++;
        }
        return $arr;
    }

    // 获取mysql数据库的版本信息
    public function get_mysql_version() {
        return mysql_get_server_info($this->connect_id);
    }

    public function close() {
        return mysql_close($this->connect_id);
    }

    public function sql_error() {
        return mysql_error();
    }
    public function sql_errno($query_id = 0) {
        $result["message"] = @mysql_error($this->connect_id);
        $result["code"]    = @mysql_errno($this->connect_id);
        return $result;
    }

    public function halt($message = '', $sql = '') {
        echo <<<EOF
            <html>
            <head>
            <title>MySQL ERROR!</title>
            </head>
            <body bgcolor="#FFFFFF">
            <table cellpadding="0" cellspacing="0" border="0" width="600" align="center" height="85%">
            <tr align="center" valign="middle">
            <td>
            <table cellpadding="10" cellspacing="0" border="0" width="80%" align="center" style="font-family: Verdana, Tahoma; color: #666666; font-size: 9px">
            <tr>
            <td valign="middle" align="center" bgcolor="#EBEBEB">
            <br /><b style="font-size: 10px">MySQL ERROR!</b>
            <br /><br /><br />$message
            <br />$sql;
            <br /><br />
            </td>
            </tr>
            </table>
            </td>
            </tr>
            </table>
            </body>
            </html>
EOF;
        exit();
    }

}
?>