register 返回的是正常的时间,但是login能够正常返回username和id,时间是0000-00-00
register 返回的是正常的时间,但是login能够正常返回username和id,时间是0000-00-00
register 返回的是正常的时间,但是login能够正常返回username和id,时间是0000-00-00
2017-06-24
<?php /** * Created by PhpStorm. * User: lee * Date: 6/18/2017 * Time: 3:20 PM */ require __DIR__.'/ErrorCode.php'; class User { /** * 数据库链接句柄 * @var */ private $_db; /** * 构造方法 * User constructor. * @param $_db */ public function __construct($_db) { $this->_db=$_db; } /** * 用户登陆 * @param $username * @param $password */ public function login($username,$password) { $sql='select * from `user` where `username`=:username and `password`=:password'; $password=$this->_md5($password); $stmt=$this->_db->prepare($sql); $stmt->bindParam(':username',$username); $stmt->bindParam(':password',$password); $stmt->execute(); $user=$stmt->fetch(PDO::FETCH_ASSOC); if(empty($user)){ throw new Exception('USERNAME_OR_PASSWORD_INVALID',ErrorCode::USERNAME_OR_PASSWORD_INVALID); } unset($user['password']); return $user; } /** * 用户注册 * @param $username * @param $password */ public function register($username,$password) { if(empty($username)){ throw new Exception('USERNAME_CANNOT_EMPTY',ErrorCode::USERNAME_CANNOT_EMPTY); } if(empty($password)){ throw new Exception('PASSWORD_CANNOT_EMPTY',ErrorCode::PASSWORD_CANNOT_EMPTY); } if($this->_isUsernameExists($username)){ throw new Exception('USERNAME_EXISTS',ErrorCode::USERNAME_EXISTS); } //写入数据库 $sql='INSERT INTO `user` (`username`,`password`,`createdAt`) values (:username,:password,:createdAt)'; $createdAt=time();//插入mysql变成00000 print_r($stmt); $password=$this->_md5($password); $stmt=$this->_db->prepare($sql); $stmt->bindParam(':username',$username); $stmt->bindParam(':password',$password); $stmt->bindParam(':createdAt',$createdAt); //echo $createdAt; //print_r($stmt); if(!$stmt->execute()){ throw new Exception('fail',ErrorCode::REGISTER_FAIL); } return [ 'userId'=>$this->_db->lastInsertId(), 'username'=>$username, 'createdAt'=>$createdAt ]; } private function _md5($string,$key='imooc') { return md5($string.$key); } private function _isUsernameExists($username) { $sql='SELECT * from `user` where `username`=:username'; $stmt=$this->_db->prepare($sql); $stmt->bindParam(':username',$username); $stmt->execute(); $result=$stmt->fetch(PDO::FETCH_ASSOC); return !empty($result); } }
举报