youBBS 用mysqli 代替了 mysql

By admin at 2013-10-31 11:09:46 • 833次点击 • 1收藏

mysqli 在性能上提升了不少,以下是 /include/mysql.class.php 的新内容,如果你换上没问题就用它吧。

<?php

if(!defined('IN_SAESPOT')) exit('Access Denied');

class DB_MySQL  {

    var $querycount = 0;
    var $link;

    function connect($servername, $dbport, $dbusername, $dbpassword, $dbname) {
        $this->link = mysqli_connect($servername, $dbusername, $dbpassword, $dbname, $dbport);
        if (mysqli_connect_errno($this->link)) {
            $this->halt("Failed to connect to MySQL: " . mysqli_connect_error());
            exit();
        }
        
        mysqli_set_charset($this->link, "utf8"); 
        mysqli_select_db($this->link, $dbname);
    }


    function geterrdesc() {
        return mysqli_connect_error();
    }

    function geterrno() {
        return intval(mysqli_connect_errno());
    }

    function insert_id() {
        return ($id = mysqli_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
    }

    function fetch_array($query, $result_type = MYSQL_ASSOC) {
        return mysqli_fetch_array($query, $result_type);
    }

    function query($sql) {
        $query = mysqli_query($this->link, $sql);
        $this->querycount++;
        return $query;
    }

    function unbuffered_query($sql) {
        $query = mysqli_query($this->link, $sql, MYSQLI_USE_RESULT);
        $this->querycount++;
        return $query;
    }

    function select_db($dbname) {
        return mysqli_select_db($dbname, $this->link);
    }

    function fetch_row($query) {
        $query = mysqli_fetch_row($query);
        return $query;
    }

    function fetch_one_array($query) {
        $result = $this->query($query);
        $record = $this->fetch_array($result);
        return $record;
    }

    function num_rows($query) {
        $query = mysqli_num_rows($query);
        return $query;
    }

    function num_fields($query) {
        return mysqli_num_fields($query);
    }

    function result($query, $row) {
        $query = @mysqli_result($query, $row);
        return $query;
    }

    function free_result($query) {
        $query = mysqli_free_result($query);
        return $query;
    }

    function version() {
        return mysqli_get_server_info($this->link);
    }

    function close() {
        return mysqli_close($this->link);
    }

    function halt($msg ='', $sql=''){
        $message = "<html>\n<head>\n";
        $message .= "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\">\n";
        $message .= "<style type=\"text/css\">\n";
        $message .=  "body,p,pre {\n";
        $message .=  "font:12px Verdana;\n";
        $message .=  "}\n";
        $message .=  "</style>\n";
        $message .= "</head>\n";
        $message .= "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#006699\" vlink=\"#5493B4\">\n";

        $message .= "<p>MySQL Database error:</p><pre><b>".htmlspecialchars($msg)."</b></pre>\n";
        $message .= "<b>Mysql error description</b>: ".htmlspecialchars($this->geterrdesc())."\n<br />";
        $message .= "<b>Mysql error number</b>: ".$this->geterrno()."\n<br />";
        $message .= "<b>Date</b>: ".date("Y-m-d @ H:i")."\n<br />";
        $message .= "<b>Script</b>: http://".$_SERVER['HTTP_HOST'].getenv("REQUEST_URI")."\n<br />";

        $message .= "</body>\n</html>";
        @header("content-Type: text/html; charset=UTF-8");
        echo $message;
        exit;
    }
}
?>

注意!若使用mysqli 则必需删掉/install 文件,或修改一下这个文件:在 define('IN_SAESPOT', 1); 上面加一行exit('installed');

youbbs, mysqli, mysql


不理解mysqli 和mysql有什么区别

root at 2013-11-02 19:31:42
1
登录 后发表评论