在PHP上使用mysqli进行紧密连接时出错

时间:2017-12-11 06:07:38

标签: php mysqli

我是PHP开发的新手,在收到电子邮件确认注册时会收到类似的警告:

  

注册成功,请使用您的数据登录

     

警告:mysqli :: close():无法获取   mysqli in   /Applications/XAMPP/xamppfiles/htdocs/Twitter/security/access.php on   第42行

这是access.php的简化代码

class access {

    public $host = null;
    public $username = null;
    public $password = null;
    public $dbname = null;
    public $conn = null;
    public $result = null;

    function __construct($xhost,$xusername,$xpassword,$xdbname) {
        $this->host = $xhost;
        $this->username = $xusername;
        $this->password = $xpassword;
        $this->dbname = $xdbname;
    }


    // connection to database
    function connect() {
        $this->conn = new mysqli($this->host,$this->username,$this->password,$this->dbname);

        if (mysqli_connect_errno()) {
            echo "Connection to database failed: ".mysqli_connect_error();
        }

        // support all languages
        $this->conn->set_charset("utf8");
    }


    public function disconnect() {

        if ($this->conn != null) {
            $this->conn->close();
        }

    }



    //saving token to the database 
    function saveTokens ($table, $id, $token) {

        $query = "INSERT INTO $table SET id=?, token=?";
        $statement = $this->conn->prepare($query);

        if (!$statement) {
            throw new Exception($statement->error);
        }

        $statement-> bind_param('is',$id,$token);
        $returnValue = $statement ->execute();

        return $returnValue;
    }


    // get userID from given Token 
    function getIDFromToken($table,$token) {

        $returnValue = [];

        $query = "SELECT id FROM $table WHERE token='$token'";
        $result = $this->conn->query($query);


        if ($result != null && (mysqli_num_rows($result)>0)) {

            $row = $result->fetch_array(MYSQLI_ASSOC);

            if (!empty($row)) {
                $returnValue = $row;
            }
        }

        return $returnValue;
    }



    function updateEmailConfirmationStatus($status,$id) {

        $query = "UPDATE users SET emailConfirmed =? WHERE id=?";

        $statement = $this->conn->prepare($query);

        if (!$statement) {
            throw new Exception($statement->error);
        }

        $statement-> bind_param('ii',$status,$id);
        $returnValue = $statement ->execute();

        return $returnValue;

    }

    // delete token when ID is available
    function deleteToken($table,$token) {

        $query = "DELETE FROM $table WHERE token =?";

        $statement = $this->conn->prepare($query);

        if (!$statement) {
            throw new Exception($statement->error);
        }

        $statement-> bind_param('s',$token);
        $returnValue = $statement ->execute();

        return $returnValue;


    }



}

我在confirmationlink.php中使用access.php 这是confirmlink.php上的代码

<?php 
require_once("../security/access.php");


if (empty($_GET["token"])) {
    echo "Please follow as per the procedure";
    return;
} 

$token = htmlentities($_GET["token"]);

// making connection to the database
$file = parse_ini_file("../../../../twitter.ini");
$dbhost = trim($file["host"]);
$dbusername = trim($file["username"]);
$dbpassword = trim($file["password"]);
$dbname = trim($file["dbname"]);

$access = new access($dbhost,$dbusername,$dbpassword,$dbname);
$access->connect();

// get ID from database as per the Token created
$info = $access->GetIDFromToken("emailTokens",$token);
$id = $info["id"];

// change emailConfirmed status to 1 if user has pressed confirmation link
$result = $access -> updateEmailConfirmationStatus(1,$id);

// menghapus token apabila ID sudah terkonfirmasi ada di database
if ($result) {
    $access ->deleteToken("emailTokens",$token);
    echo "Registration success, please login using your data";
}

// close the connection
$access -> disconnect();

我只在confirmlink.php文件的最后一行收到错误。数据库已根据需要在MySQL上成功更改。我也在其他PHP文件上使用disconnect方法,但我没有问题。但不知怎的,我现在得到了这个问题

这里出了什么问题?在此先感谢:)

1 个答案:

答案 0 :(得分:0)

在每个函数中返回结果之前,需要关闭语句:

$statement->close();