当我尝试通过verify_user方法验证用户数据
<?php
public static function verify_user ($username , $password)
{
global $database;
$username = $database->escape_string($username);
$password = $database->escape_string($password);
$query = "SELECT * FROM users WHERE username = '{$username}' and password = '{$password}' LIMIT 1";
$result_array = self::find_this_query($query);
if (!empty($result_array)) {
$result_array = array_shift($result_array);
return $result_array;
} else {
return false;
}
}
这也是我的find_this_query方法
public static function find_this_query ($enter_here_your_sql_query)
{
global $database;
$result = $database->query($enter_here_your_sql_query);
return $result;
}
并尝试在此处登录我的login.php代码的用户
<?php
if($session->is_signed_in()) {
redirect("index.php");
}
if (isset($_POST['submit'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
}
//method to check db user
$user_found = User::verify_user($username , $password);
if ($user_found) {
$session->login($user_found);
redirect("index.php");
} else {
$the_message = "<p class='alert alert-warning' style='color: grey'>Your password or username is incorrect</p>";
$username = "";
$password = "";
}
?>
我在登录页面上看到该错误
警告:array_shift()期望参数1为数组,第189行的/admin/includes/User.php中给出的对象
这是user.php类的第189行
$result_array = array_shift($result_array);
我在这里做错了什么?
答案 0 :(得分:0)
$ database-> query()不返回数组,它返回一个对象。尝试使用$database->fetch_array()。
答案 1 :(得分:0)
mysqli::query()
函数不返回数组,而是返回一个mysqli_result
对象。
mysqli_result
实现了Traversable
接口,该接口允许您像foreach
一样在其上使用 some 数组操作,但是大多数其他数组操作不起作用。特别是,不能在该对象上使用array_shift()
。
代替
$result_array = array_shift($result_array);
return $result_array;
使用:
$row = $result_array->fetch_assoc();
return $row;
答案 2 :(得分:0)
我可以通过“帮助”给我Barmar解决该问题。
这是我更新的verify_user函数 公共静态函数verify_user($ username,$ password) { 全局$ database;
$username = $database->escape_string($username);
$password = $database->escape_string($password);
$query = "SELECT * FROM users WHERE username = '{$username}' and password = '{$password}' LIMIT 1";
$result_array = self::find_this_query($query);
if (!empty($result_array)) {
$result_array = $result_array->fetch_array();
return $result_array;
}else{
return false;
}
}
这是我更新的login.php
if($session->is_signed_in()) {
redirect("index.php");
}
if (isset($_POST['submit'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
}
//method to check db user
$user_found = User::verify_user($username , $password);
$user = User::instantiation($user_found);
$user_id = $user->id;
if ($user_id){
$session->login($user);
redirect("index.php");
} else {
$the_message = "<p class='alert alert-warning' style='color: grey'>Your password or username is incorrect</p>";
$username = "";
$password = "";
}