<?php

class db {
    var $link;
    var $db_host;
    var $db_user;
    var $db_pass;
    var $db_name;
    var $db_port;
    var $conn;

    public function __construct($db_host, $db_user, $db_pass, $db_name, $db_port = 3306) {
        $this->db_host = $db_host;
        $this->db_user = $db_user;
        $this->db_pass = $db_pass;
        $this->db_name = $db_name;
        $this->db_port = $db_port;
    }

    public function connect() {
        if (class_exists('mysqli')) {
            $this->conn = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name, $this->db_port);
            if ($this->conn->connect_error) {
                die('数据库连接失败: ' . $this->conn->connect_error);
            }
            $this->conn->set_charset('utf8');
        } elseif (function_exists('mysql_connect')) {
            $this->conn = mysql_connect($this->db_host . ':' . $this->db_port, $this->db_user, $this->db_pass);
            if (!$this->conn) {
                die('数据库连接失败');
            }
            mysql_select_db($this->db_name, $this->conn);
            mysql_set_charset('utf8', $this->conn);
        } else {
            die('不支持的数据库类型');
        }
    }

    public function query($sql) {
        if (class_exists('mysqli')) {
            $result = $this->conn->query($sql);
            if ($result === false) {
                return false;
            }
            return $result;
        } elseif (function_exists('mysql_query')) {
            $result = mysql_query($sql, $this->conn);
            return $result;
        }
    }

    public function query_multi($sql) {
        if (class_exists('mysqli')) {
            if ($this->conn->multi_query($sql)) {
                return true;
            }
            return false;
        }
        return false;
    }

    public function get_row($sql) {
        if (class_exists('mysqli')) {
            $result = $this->conn->query($sql);
            if ($result && $result->num_rows > 0) {
                return $result->fetch_assoc();
            }
            return false;
        } elseif (function_exists('mysql_query')) {
            $result = mysql_query($sql, $this->conn);
            if ($result && mysql_num_rows($result) > 0) {
                return mysql_fetch_assoc($result);
            }
            return false;
        }
    }

    public function get_all($sql) {
        $data = array();
        if (class_exists('mysqli')) {
            $result = $this->conn->query($sql);
            if ($result && $result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $data[] = $row;
                }
            }
            return $data;
        } elseif (function_exists('mysql_query')) {
            $result = mysql_query($sql, $this->conn);
            if ($result && mysql_num_rows($result) > 0) {
                while ($row = mysql_fetch_assoc($result)) {
                    $data[] = $row;
                }
            }
            return $data;
        }
        return $data;
    }

    public function fetch($result) {
        if (class_exists('mysqli')) {
            if ($result instanceof mysqli_result) {
                return $result->fetch_assoc();
            }
            return false;
        } elseif (function_exists('mysql_fetch_assoc')) {
            return mysql_fetch_assoc($result);
        }
    }

    public function count($sql) {
        if (class_exists('mysqli')) {
            $result = $this->conn->query($sql);
            if ($result) {
                $row = $result->fetch_row();
                return $row ? intval($row[0]) : 0;
            }
            return 0;
        } elseif (function_exists('mysql_query')) {
            $result = mysql_query($sql, $this->conn);
            if ($result) {
                $row = mysql_fetch_row($result);
                return $row ? intval($row[0]) : 0;
            }
            return 0;
        }
        return 0;
    }

    public function num_rows($result) {
        if (class_exists('mysqli')) {
            return $result->num_rows;
        } elseif (function_exists('mysql_num_rows')) {
            return mysql_num_rows($result);
        }
        return 0;
    }

    public function insert($sql) {
        if (class_exists('mysqli')) {
            $this->conn->query($sql);
            return $this->conn->insert_id;
        } elseif (function_exists('mysql_query')) {
            mysql_query($sql, $this->conn);
            return mysql_insert_id($this->conn);
        }
    }

    public function insert_array($table, $array) {
        $fields = array_keys($array);
        $values = array_values($array);
        $fields = array_map(function($v) { return "`{$v}`"; }, $fields);
        $values = array_map(function($v) { return "'{$v}'"; }, $values);
        $sql = "INSERT INTO `{$table}` (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")";
        return $this->insert($sql);
    }

    public function insert_id() {
        if (class_exists('mysqli')) {
            return $this->conn->insert_id;
        } elseif (function_exists('mysql_insert_id')) {
            return mysql_insert_id($this->conn);
        }
    }

    public function affected() {
        if (class_exists('mysqli')) {
            return $this->conn->affected_rows;
        } elseif (function_exists('mysql_affected_rows')) {
            return mysql_affected_rows($this->conn);
        }
    }

    public function escape($str) {
        if (class_exists('mysqli')) {
            return $this->conn->real_escape_string($str);
        } elseif (function_exists('mysql_real_escape_string')) {
            return mysql_real_escape_string($str, $this->conn);
        }
        return addslashes($str);
    }

    public function error() {
        if (class_exists('mysqli')) {
            return $this->conn->error;
        } elseif (function_exists('mysql_error')) {
            return mysql_error($this->conn);
        }
    }

    public function close() {
        if (class_exists('mysqli')) {
            $this->conn->close();
        } elseif (function_exists('mysql_close')) {
            mysql_close($this->conn);
        }
    }
}
?>
