【问题标题】:Updating php code from mysql to mysqli, undefined variable but its defined in the constructor将 php 代码从 mysql 更新到 mysqli,未定义的变量,但它在构造函数中定义
【发布时间】:2015-05-24 23:19:05
【问题描述】:

我从 androidhive 下载了这段代码。这是安卓应用程序的登录/注册代码。现在的php代码是两年前的,所以我最初遇到折旧错误,所以我开始将代码更新为mysqli。到目前为止,我一直在将mysql函数更改为mysqli,如果需要调整参数.一般来说,我对php相当陌生,所以我遇到了一些问题,主要是DB_functions中的isUserExisted函数,变量db对于mysqli_query是未定义的。我不确定为什么会收到此错误,因为我已在构造函数中定义了它。我看过类似的问题,解决问题的答案通常是语法错误,当我比较这段代码时,我似乎没有。任何帮助将不胜感激。下面分别是 DB_Functions.php 和 DB_Connect.php:

<?php

class DB_Functions {

	private $db = null;

    //put your code here
    // constructor
    function __construct() {
        require 'DB_Connect.php';
		
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connectThis();
		
    }

    // destructor
    function __destruct() {
        
    }

    /**
     * Storing new user
     * returns user details
     */
    public function storeUser($name, $email, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
        $result = mysqli_query($db,"INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
        // check for successful store
        if ($result) {
            // get user details 
            $uid = mysqli_insert_id(); // last inserted id
            $result = mysqli_query($db,"SELECT * FROM users WHERE uid = $uid");
            // return user details
            return mysqli_fetch_array($result);
        } else {
            return false;
        }
    }

    /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($email, $password) {
        $result = mysqli_query($db,"SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
        // check for result 
        $no_of_rows = mysqli_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysqli_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['encrypted_password'];
            $hash = $this->checkhashSSHA($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return false;
        }
    }

    /**
     * Check user is existed or not
     */
    public function isUserExisted($email) {
        $result = mysqli_query($db,"SELECT email from users WHERE email = '$email'");
        $no_of_rows = mysqli_num_rows($result);
        if ($no_of_rows > 0) {
            // user existed 
            return true;
        } else {
            // user not existed
            return false;
        }
    }

    /**
     * Encrypting password
     * @param password
     * returns salt and encrypted password
     */
    public function hashSSHA($password) {

        $salt = sha1(rand());
        $salt = substr($salt, 0, 10);
        $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
        $hash = array("salt" => $salt, "encrypted" => $encrypted);
        return $hash;
    }

    /**
     * Decrypting password
     * @param salt, password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) {

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    }

}

?>

<?php
class DB_Connect {
	

    // constructor
    function __construct() {
        
    }

    // destructor
    function __destruct() {
        // $this->close();
    }

    // Connecting to database
    public function connectThis() {
        require 'include/Config.php';
        // connecting to mysql
        $db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());
		// selecting database

        // return database handler
        return $db;
    }

    // Closing database connection
    public function close() {
        mysqli_close();
    }

}

?>

【问题讨论】:

    标签: php android mysql mysqli undefined


    【解决方案1】:

    你需要这样称呼它:

    $result = mysqli_query($this->db,"SELECT email from users WHERE email = '$email'");
    

    【讨论】:

    • 您好,我按照您的建议做了,但我仍然收到错误:注意:未定义变量:db 但现在我也收到:致命错误:无法访问空属性,这是我之前没有得到的.是我与数据库的连接吗?
    • 哎呀意识到我有一个 $this->$db,而不是 $this->db... 但现在错误是:警告:mysqli_query() 期望参数 1 是 mysqli
    【解决方案2】:

    我终于明白了!在使用 No-nonsense 的建议解决了我原来的问题后,我立即遇到了 mysqli_query 需要“mysqli”的错误,而是得到了一个对象。我从对互联网的评分中意识到,这是因为我在 DB_function.php 的构造函数中声明了我的连接,这使它成为一个对象,从而产生了错误。我不认为这是最好的方法,但是对于需要连接到数据库的每个函数,我都写了:

    $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());
    

    在 DB_Functions.php 的每个函数中,并在调用 mysqli_query 的地方将 $conn 作为第一个参数传递。代码现在完美运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-24
      • 2013-01-05
      • 1970-01-01
      • 2012-08-31
      • 2018-01-29
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多