使用准备好的语句更新记录,检查更新是否有效
问题描述:
我有一个查询来更新数据库中的记录,它工作正常,但我想知道如何检查更新是否已发生,以便我可以返回true并显示正确的消息?
I have a query that updates a record on my database, it works fine but i wanted to know how to check if the update has happened so i can return true and display the right message?
现在,我知道我可以执行SELECT查询:
Now i know with a SELECT query i can do:
if(stmt->fetch())
如果是这样,我将返回true并说找到记录",但是我不知道如何针对更新查询执行此操作?
If that is true i return true and saying "records found" but i haven't got a clue how to do it for an update query?
有人知道怎么做吗?
$query = "UPDATE user
SET password = ?
WHERE email = ?";
if($stmt = $conn->prepare($query))
{
$stmt->bind_param('ss', $pwd, $userEmail);
$stmt->execute();
//Check if update worked
}
感谢您的帮助.
答
检查以下内容是否有效
$query = "UPDATE user
SET password = ?
WHERE email = ?";
if($stmt = $conn->prepare($query))
{
$stmt->bind_param('ss', $pwd, $userEmail);
if ($stmt->execute())
{
// worked
}
else
{
// not worked
}
祝你好运!