我从androidhive下载了这段代码。它是Android应用程序的登录/注册代码。现在php代码来自两年前,所以我最初得到了折旧错误,所以我开始将代码更新到mysqli.So远在转换时,我已经将mysql函数更改为mysqli,如果需要调整参数。我一般都对php很新,所以我遇到了一些问题,主要的问题是DB_functions中isUserExisted函数的问题,mysqli_query的变量db是未定义的。我不确定为什么我会收到此错误,因为我已在构造函数中定义了它。我已经查看了类似的问题,修复问题的答案通常是语法错误,当我比较这些代码时,我似乎没有。任何帮助将不胜感激。这里分别是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;
}
}
?>
&#13;
<?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();
}
}
?>
&#13;
答案 0 :(得分:0)
你需要这样称呼它:
$result = mysqli_query($this->db,"SELECT email from users WHERE email = '$email'");
答案 1 :(得分:0)
我终于明白了!在使用了解决我原来问题的严肃的建议之后,我立即遇到了mysqli_query需要&#34; mysqli&#34;的错误,但却得到了一个对象。我通过对互联网的评分意识到这是因为我在DB_function.php的构造函数中声明了我的连接,因此它使它成为一个对象,从而产生错误。我不认为这是最好的方法,但是对于每个需要连接数据库的函数我写道:
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());
在DB_Functions.php的每个函数中,并将$ conn作为调用mysqli_query的第一个参数传递。代码现在完美无缺。