PHP错误:致命错误:未捕获错误:调用未定义方法stdClass :: update()出了什么问题?

时间:2019-03-09 21:35:45

标签: php mysql mysqli

我不明白为什么我在运行以下代码时会收到该错误

$user = $user->find_user_by_id_oop(2);
$user->password = "Pass";
$user->update();

这是我的find_user_by_id_oop方法现场用户类

public function find_user_by_id_oop($user_id) {
    global $database;
    $result = $database->query("SELECT * FROM users WHERE id= {$user_id} LIMIT 1");
    $found_user = $result->fetch_object();

    return $found_user;
}

这也是我在User类中的更新方法

public function update($userID, $username, $password, $first_name, $last_name, $email) {
    global $database;
    global $session;
    $name = trim($database->escape_string($username));
    $pass = trim($database->escape_string($password));
    $firstname = trim($database->escape_string($first_name));
    $lastname = trim($database->escape_string($last_name));
    $email_address = trim($database->connection->real_escape_string($email));

    $query = "UPDATE users SET username='$name' , password='$pass' , first_name='$firstname' , last_name='$lastname' , email='$email_address' where id=$userID";
    $res = $database->query($query);

    if ($res) {
        $session->message('Data Updated');
        return $res;
    } else {
        $session->message('Data Not Updated');
    }
}

我的课上也有那个变量

public $username, $id, $first_name, $last_name, $password;

并且我在类括号之外实例化类

$user = new User();

1 个答案:

答案 0 :(得分:0)

我的问题已通过fetch_object(User :: class);上的传递类名解决了

下面的固定功能

   public static function find_user_by_id_oop($user_id)
{
    global $database;
    $result = $database->query("SELECT * FROM users WHERE id= {$user_id} LIMIT 1");
    return $result->fetch_object(User::class);

}

我在这里找到了相关信息:mysqli_result::fetch_object

class_name

要实例化的类的名称,设置的属性并返回。如果未指定,则返回stdClass对象。