我从先前的实例化静态方法调用方法时遇到问题。
静态方法是从用户那里获取ID,之后我必须更改他的密码。
我确实喜欢这个:
$user = user::find_user_by_id(2);
$user->password = "asdasd";
$user->update();
我收到错误:
Uncaught Error: Call to undefined method stdClass::update()
我明白问题是什么,但无法弄清楚如何解决这个问题。
这是更新方法:
public function update(){
$db = db::getConnection()->conn;
$sql = "UPDATE ".self::$table." SET username=':username', password = ':password' where id =':id'";
$stmt = $db->prepare($sql);
$stmt->bindParam(':username',$this->username);
$stmt->bindParam(':password',$this->password);
$stmt->bindParam(':password',$this->id);
if($stmt->execute()){
return true;
}else{
return false;
}
}
按ID
查找用户public static function find_user_by_id($user_id){
$result_set = self::find_query("SELECT * FROM ".self::$table." where id = {$user_id} LIMIT 1");
$found_user = $result_set->fetch(PDO::FETCH_OBJ);
return $found_user;
}