PHP / MySQL - 没有捕获mysqli_sql_exception:MySQL服务器已经消失了

时间:2015-11-13 07:46:31

标签: php mysql session

我有以下代码,它在XAMPP服务器上运行良好。现在我将PHP-Code和MySQL-Database复制到一个新的服务器上,手动安装了Apache,PHP和MySQL。

我收到以下错误:

  

警告:未捕获mysqli_sql_exception:MySQL服务器已经消失了   C:\ intranet \ apache24 \ htdocs \ content \ php \ session.class.php:87

     

堆栈追踪:

     

#0 C:\ intranet \ apache24 \ htdocs \ content \ php \ session.class.php(87):   mysqli_stmt-> execute()

     

#1 [内部功能]:   session-> read('3r7jkgjk93pu9fc ...')

     

#2 C:\ intranet \ apache24 \ htdocs \ content \ php \ session.class.php(43):   session_regenerate_id(true)

     

#3 C:\ intranet \ apache24 \ htdocs \ content \ php \ session.class.php(19):   session-> start_session('_ sichereSitzung ...',false)

     

#4 C:\ intranet \ apache24 \ htdocs \ content \ php \ session.php(16):   session-> __ construct()

     

#5 C:\ intranet \ apache24 \ htdocs \ index.php(21):   require_once('C:\ intranet \ apa ...')

     

#6 {main}投入   第87行的C:\ intranet \ apache24 \ htdocs \ content \ php \ session.class.php   可捕获的致命错误:session_regenerate_id():无法创建   会话ID:用户(路径:)   C:\内部网\ apache24 \ htdocs中\内容\ PHP \ session.class.php

第43r行代码

这是我的代码:

class session {

    function __construct() {
        // set custom session functions
        session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'gc'));

       // this line prevents unexpected effects when using objects as save handler
       register_shutdown_function('session_write_close');

       // start the session
       $this->start_session('_sichereSitzung-A3i6Zz', FALSE);
    }

    function start_session($session_name, $secure) {
        // make sure the session cookie is not accessible via javascript
        $httponly = true;

        // get session cookie parameters 
        $cookieParams = session_get_cookie_params(); 

        // set the parameters
        session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);

        // prevent javascript from getting session id
        ini_set('session.use_only_cookies', 1);

        // change the session name 
        session_name($session_name);

        // now we can start the session
        session_start();

        // this line regenerates the session and delete the old one
        // it also generates a new encryption key in the database
        session_regenerate_id(true); 
    }

    function open() {
        // configuration file
        require_once('config.php');     

        // the sql data
        require_once(PHP_PATH . 'sqluserdata.php');

        //open mysqli-connection
        $mysqli = new mysqli(SESS_HOST, SESS_USER, SESS_PW, SESS_DB, SESS_PORT);

        //bind the mysqli-connection to variable
        $this->db = $mysqli;

        return true;
    }

    function close() {
        // close mysqli-connection
        $this->db->close();

        return true;
    }

    function read($id) {
        $min=1;
        $max=200;
        $rand = mt_rand($min,$max);

        // only run garbage collection random on page load
        if ($rand == 1) {
            // garbage collection
            $this->gc(259200);
        }

        // if statement doesnt exists create statement
        if (!isset($this->read_stmt)) {
            $this->read_stmt = $this->db->prepare("SELECT data FROM session WHERE id = ? LIMIT 1");
        }

        // bind session parameter, run statement, bind result to variable
        $this->read_stmt->bind_param('s', $id);
        $this->read_stmt->execute();
        $this->read_stmt->store_result();
        $this->read_stmt->bind_result($data);
        $this->read_stmt->fetch();

        return $data;
    }

    function write($id, $data) {
        // bind time to variable
        $time = time();

        // if statement doesnt exists, prepare statement
        if(!isset($this->w_stmt)) {
            $this->w_stmt = $this->db->prepare("REPLACE INTO session (id, access, data) VALUES (?, ?, ?)");
        }

        // bind variables, execute statement
        $this->w_stmt->bind_param('sis', $id, $time, $data);
        $this->w_stmt->execute();

        return true;
    }

    function destroy($id) {
        // if statement doesnt exists, prepare statement
        if(!isset($this->delete_stmt)) {
            $this->delete_stmt = $this->db->prepare("DELETE FROM session WHERE id = ?");
        }

        // bind parameters, execute statement
        $this->delete_stmt->bind_param('s', $id);
        $this->delete_stmt->execute();

        return true;
    }

    function gc($max) {
        // if statement doesnt exists, prepare statement
        if(!isset($this->gc_stmt)) {
            $this->gc_stmt = $this->db->prepare("DELETE FROM session WHERE access < ?");
        }

        // calculate time to delete
        $old = time() - $max;

        // bind parameters, execute query
        $this->gc_stmt->bind_param('s', $old);
        $this->gc_stmt->execute();

        return true;
    }

}

有人知道这个错误吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

此错误与您的PHP代码无关 - 这是MySQL发送的错误。

基本上,MySQL server has crashed

  

MySQL服务器最常见的原因已经消失了   服务器超时并关闭连接。在这种情况下,你   通常会得到以下错误代码之一(你得到的是哪一个)   取决于操作系统。)

查看我链接到的页面中可能存在的问题。常见的问题是:

  • 您正在执行服务器无法处理的非常复杂的查询;
  • 您的MySQL服务器配置错误。

运行查询时是否始终发生错误?或者它是一些特定的查询它崩溃了什么?你应该先检查一下。

相关问题