使用准备好的语句更新记录,检查更新是否有效 [英] updating records with prepared statements, checking if update worked

查看:50
本文介绍了使用准备好的语句更新记录,检查更新是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询来更新数据库中的记录,它工作正常,但我想知道如何检查更新是否已发生,以便我可以返回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
}

祝你好运!

这篇关于使用准备好的语句更新记录,检查更新是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆