我以前常常使用MSSQL,但最近不得不使用MYSQL,我知道它很相似,但事情的完成方式让我感到困惑。
我的问题是在我调用存储过程(插入,更新,选择,删除)之后如何在从函数返回数据之前在PHP中检查调用是否成功?
来自PHP的CURRENT MYSQL(SELECT)调用:
public function login($email){
$stmt = $this->connection->prepare('call loginUser(?)');
$stmt->bind_param("s", $email);
$stmt->execute();
// if($stmt === false){
// return false; HOW I CURRENTLY DO IT IN MSSQL
// }
$result = $stmt->get_result();
$row = mysqli_fetch_assoc($result);
$stmt->close();
return json_encode(array("success"=>true));
}
来自PHP的CURRENT MYSQL(INSERT)调用:
public function registerUser($params){
// Prepare and Bind
$stmt = $this->connection->prepare('call addUser(?,?,?,?,?)');
$stmt->bind_param("sssss", $firstname, $surname, $displayname, $email, $password);
$firstname = $params['firstname'];
$surname = $params['surname'];
$displayname = $params['firstname'] . ' ' . $params['surname'];
$email = $params['email'];
$password = password_hash($params['password'], PASSWORD_DEFAULT);
$stmt->execute();
$stmt->close();
return json_encode(array("success"=>true));
}
答案 0 :(得分:0)
试试这个
public function registerUser($params= null){
// Prepare and Bind
if ($param==0) {
echo '<script>alert(\'saved\')</script>';
$stmt = $this->connection->prepare('call addUser(?,?,?,?,?)');
$stmt->bind_param("sssss", $firstname, $surname, $displayname, $email, $password);
$firstname = $params['firstname'];
$surname = $params['surname'];
$displayname = $params['firstname'] . ' ' . $params['surname'];
$email = $params['email'];
$password = password_hash($params['password'], PASSWORD_DEFAULT);
$stmt->execute();
$stmt->close();
}
}
答案 1 :(得分:0)
执行返回bool值,你可以这样检查
if (! $stmt->execute()) { return false; }