定义未定义的结果mysql_free_result

时间:2016-07-26 13:30:15

标签: php mysqli

我正在尝试释放一个mysqli结果,变量已定义,然后是未定义的,我不知道发生了什么,但是太奇怪了。

public function __destruct() {
    if(isset($this->result) && is_a($this->result, "mysqli_result")) {
        var_dump($this->result);
        mysqli_free_result($this->$result);
    }
    mysqli_close($this->conexion);
}

如果是isset并且是一个mysqli_result类型,它进入条件,它进入,我得到这个:

Notice: Undefined variable: result in C:\xampp\htdocs\Proyectos\Tell Yourself\app\model\Database.class.php on line 22

这是完整的课程:

abstract class Database {
protected $conexion;
protected $result;

public function __construct() {
    if(!isset($this->conexion)) {
        if($this->conexion = mysqli_connect("localhost", "root", "", "tellYourself")) {
            return true;
        }
        else {
            echo "No se ha podido realizar conexion con la base de datos: ".mysqli_connect_error();
            return false;
        }
    }
}

public function __destruct() {
    if(isset($this->result) && is_a($this->result, "mysqli_result")) {
        var_dump($this->result);
        mysqli_free_result($this->$result);
    }
    mysqli_close($this->conexion);
}

protected function query($query) {
    if($this->result = mysqli_query($this->conexion, $query)) {
        /*if(is_a($this->result, "mysqli_result")) {
            $this->result = mysqli_fetch_object($this->result);
        }*/
        return $this->result;
    }
    else {
        return false;
    }
}
}

这就是孩子:

require_once("app/model/Database.class.php");

class User extends Database {

public function __construct() {
    parent::__construct();
}

public function __destruct() {
    parent::__destruct();
}

public function login($email, $password) {
    $email = mysqli_real_escape_string($this->conexion, $email);
    $password = md5($password);

    $loginQuery = "SELECT id, username FROM users WHERE email='$email' and password='$password';";
    if($result = $this->query($loginQuery)) {
        $result = mysqli_fetch_object($result);
        $_SESSION["username"] = $result->username;
        return true;
    }
    else {
        return false;
    }
}

public function register($username, $email, $password) {
    $username = mysqli_real_escape_string($this->conexion, $username);
    $email = mysqli_real_escape_string($this->conexion, $email);
    $password = md5($password);
    $activationCode = md5($username.rand(0, 100));

    $registerQuery = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password');";
    if($this->query($registerQuery)) {
        $activationCodeQuery = "INSERT INTO user_emailconfirm(userId, activeCode) VALUES(".mysqli_insert_id($this->conexion).", '$activationCode');";
        if($this->query($activationCodeQuery)) {
            return array("userID" => mysqli_insert_id($this->conexion), "activationCode" => $activationCode);
        }
    }
    else {
        return false;
    }
}

public function activateUser($activationCode, $userID) {
    $activateUserQuery = "SELECT userId, activeCode FROM user_emailconfirm WHERE userId='$userID' and activeCode='$activationCode';";
    if($result = $this->query($activateUserQuery)) {
        var_dump($result);
        if(mysqli_num_rows($result) == 1) {
            $activateUserQuery = "UPDATE user_emailconfirm SET confirmed=1 WHERE userId='$userID' and activeCode='$activationCode';";
            if($this->query($activateUserQuery)) {
                return true;
            }
        }
    }
    else {
        return false;
    }
}
}

代码的最终结果是在有数据时释放结果。

1 个答案:

答案 0 :(得分:3)

我不确定,但如果你刚刚复制了上面的代码,我看到了一个错误: 更改以下行(在析构函数中):

mysqli_free_result($this->$result);

mysqli_free_result($this->result);

删除mysqli_free_result()参数中'result'之前的$ -sign。 否则,php会将该属性解释为动态变量,并且未定义。

我希望它能解决错误,我可以提供帮助:)。