PHP警告:mysqli :: close():无法获取mysqli

时间:2014-04-10 11:00:54

标签: javascript php jquery mysql mysqli

我正在设置一个显示来自MySQL数据库的短信的网络应用程序。

当我运行代码时,我得到PHP警告:mysqli :: close():无法在第29行的/Applications/MAMP/htdocs/class-functions.php中获取mysqli,而Firefox则会出现500内部服务器错误。< / p>

目录如下:

  • /ajax/train-check.php
  • /js/jquery-2.1.0.min.js
  • /js/train-script.js
  • /class-functions.php
  • 的index.php
  • /insert.php

“index.php”调用“train-script.js”:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="js/jquery-2.1.0.min.js" type="text/javascript"></script>
<script src="js/train-script.js" type="text/javascript"></script>
</head>
<body>
<div id="wrapper">
<div class="clear"></div>
<div id="page">
    <div class="border">
        <div id="train-input" class='maintable'>
        </div>
    </div>
</div>
</div>
</body>
</html>

“train-script.js”每秒运行一次,并通过“train-check.php”检查数据库中的新行:

$(document).ready(function() {
triggerTimer();
});

var timerId = null;

function triggerTimer() {
if (timerId != null) {
    clearTimeout(timerId);
}

timerId = setTimeout(function(){
    $.ajax({
        url: '../ajax/train-check.php',
        success: function(data) {
            $('#train-input').prepend(data);
            triggerTimer();
        },
        error: function() {
            triggerTimer();
        }
    });
}, 1000);
}

“train-check.php”看起来像:

<?php

include('../class-functions.php');

$train = new train_functions();
echo $train->get_data($train->check_data());

?>

最后的class-functions.php(在php-error中声明的第29行是:$ this-&gt; db_link-&gt; close();)

<?php


class train_functions {

private $host="localhost";
private $username="root";
private $password="root";
private $db_name="pitscreen";
private $db_link;

function __construct() {
    $this->db_connect();
}

function __destruct(){
    $this->db_disconnect();
}

private function db_connect(){
    $this->db_link = new mysqli($this->host, $this->username, $this->password, $this->db_name);
    if ($this->db_link->connect_errno) {
        printf("Connect failed: %s\n", $this->db_link->connect_error);
        exit();
    }
}

private function db_disconnect(){
    $this->db_link->close();
}

public function check_data(){
    $query = "SELECT * FROM received_sms ORDER by received_time DESC LIMIT 13";
    $result = $this->db_link->query($query);
    error_log(var_export($result,true));
    $data = $this->db_link->fetch_array($result);
    error_log($this->db_link->error());
    if(empty($data)){
        error_log('Could not fetch data from db '.mysql_error());
        return null;
    } else {
        return $data;
    }
}

public function get_data($data){
    $return = "<div class='msgtable'>";
    $return .= "<div class='msg'>". $data["msg"]."</div>";
    $return .= "<div class='from'>Fr&aring;n:". $data["msisdn"]. "|" . $data["received_time"]."</div></div><div class='hr'><hr /></div>";
    return $return;
}

public function insert_data($originator,$msisdn,$msg){
    $sql="INSERT INTO $this->db_name(originator, msisdn, msg, received_time)VALUES('$originator', '$msisdn', '$msg', now())";
    mysql_query($sql,$this->db_link);
}
}
?>

PHP警告:mysqli :: close():无法在第29行的/Applications/MAMP/htdocs/class-functions.php中获取mysqli。

有没有人在代码中看到任何明显的错误?我现在迷路了,任何意见都非常感谢!

//弗雷德里克

0 个答案:

没有答案