MariaDB服务器版本为10.1.21。
下面是我的User类,它在User类中用于将用户添加到数据库。虽然添加了用户,但在将用户保存到数据库时,execute()
方法似乎不会返回true
。
<?php
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh; // DB handler
private $error;
private $stmt;
public function __construct() {
// set DSN i. e. data source name
$dsn = "mysql:host=".$this -> host.";dbname=".$this -> dbname;
// set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// create a new PDO instance
try {
$this -> dbh = new PDO($dsn, $this -> user, $this -> pass, $options); // connector to DB
} catch(PDOException $ex) {
$this -> error = $ex -> getMessage();
}
}
public function query($query) {
$this -> stmt = $this -> dbh -> prepare($query);
}
public function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this -> stmt -> bindValue($param, $value, $type);
}
public function execute() {
$this -> stmt -> execute();
}
public function resultset() {
$this -> execute();
return $this -> stmt -> fetchAll(PDO::FETCH_OBJ); // FETCH_OBJ to fetch object from DB. Could be associative array
}
public function single() {
$this -> execute();
return $this -> stmt -> fetch(PDO::FETCH_OBJ);
}
public function rowCount() {
return $this -> stmt -> rowCount();
}
public function lastInsertId() {
return $this -> dbh -> lastInsertId();
}
public function beginTransaction() {
return $this -> dbh -> beginTransaction();
}
public function endTransaction() {
return $this -> dbh -> commit();
}
public function cancelTransaction() {
return $this -> dbh -> rollback();
}
}
用户类代码如下。 execute()
中的public function register($data)
方法似乎不会返回true
,只会在另一个代码段中执行false
个案。通过public function register($data)
上方的return true;
缩短if ($this -> db -> execute()) {...}
短片可以接收success
消息。
<?php
class User {
private $db;
public function __construct() {
$this -> db = new Database();
}
// register user
public function register($data) {
$this -> db -> query("INSERT INTO users (name, email, avatar, username, password, about, last_activity) VALUES (:name, :email, :avatar, :username, :password, :about, :last_activity)");
$this -> db -> bind(':name', $data['name']);
$this -> db -> bind(':email', $data['email']);
$this -> db -> bind(':avatar', $data['avatar']);
$this -> db -> bind(':username', $data['username']);
$this -> db -> bind(':password', $data['password']);
$this -> db -> bind(':about', $data['about']);
$this -> db -> bind(':last_activity', $data['last_activity']);
if ($this -> db -> execute()) {
return true;
} else {
return false;
}
}
// upload user avatar
public function uploadAvatar() {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES['avatar']['name']);
$extension = end($temp);
if ((($_FILES['avatar']['type'] == "image/gif")
|| ($_FILES['avatar']['type'] == "image/jpeg")
|| ($_FILES['avatar']['type'] == "image/jpg")
|| ($_FILES['avatar']['type'] == "image/pjpeg")
|| ($_FILES['avatar']['type'] == "image/x-png")
|| ($_FILES['avatar']['type'] == "image/png"))
&& ($_FILES['avatar']['size'] < 50000)
&& in_array($extension, $allowedExts)) {
if ($_FILES['avatar']['error'] > 0) {
redirect('register.php', $_FILES['avatar']['error'], 'error');
} else {
if (file_exists('images/avatars/'.$_FILES['avatar']['name'])) {
redirect('register.php', "File already exists", 'error');
} else {
move_uploaded_file($_FILES['avatar']['tmp_name'], 'images/avatars/'.$_FILES['avatar']['name']);
return true;
}
}
} else {
redirect('register.php', "Invalid file type", 'error');
}
}
// user log in
public function login($username, $password) {
$this -> db -> query("SELECT * FROM users WHERE username = :username AND password = :password");
$this -> db -> bind(':username', $username);
$this -> db -> bind(':password', $password);
$row = $this -> db -> single();
if ($this -> db -> rowCount() > 0) {
$this -> setUserData($row);
return true;
} else {
return false;
}
}
// set user data
public function setUserData($row) {
$_SESSION['is_logged_in'] = true;
$_SESSION['user_id'] = $row -> id;
$_SESSION['username'] = $row -> username;
$_SESSION['name'] = $row -> name;
}
// log user out
public function logout() {
unset($_SESSION['is_logged_in']);
unset($_SESSION['user_id']);
unset($_SESSION['username']);
unset($_SESSION['name']);
return true;
}
// get total number of users
public function getTotalUsers() {
$this -> db -> query("SELECT * FROM users");
$this -> db -> resultset();
return $this -> db -> rowCount();
}
}
上面的false
案例在另一个代码段中处理:
if ($user -> register($data)) {
redirect('index.php', 'You are registered and can now log in', 'success');
} else {
redirect('index.php', 'Something went wrong with the registration', 'error');
}
收到的唯一消息是error
。
请建议。我看不出代码有什么问题。用户得到了很好的补充。
答案 0 :(得分:1)
您的Database
execute()
方法不返回任何内容。 NULL
是false
。所以添加return
语句:
public function execute() {
return $this -> stmt -> execute();
}