确定SQL UPDATE是否更改了列的值
16lz
2021-01-22
I have a table with a text column called myTextColumn. I would like to do something in the PHP application only if an UPDATE changed the value. I am not using triggers. Is there a better way to do so other than the following? Thank you
我有一个表格,其中包含一个名为myTextColumn的文本列。我想只在UPDATE更改了值时才在PHP应用程序中执行某些操作。我没有使用触发器。除了以下之外,还有更好的方法吗?谢谢
function updateTable($data)
{
$sql='SELECT id FROM myTable WHERE id=:id AND myTextColumn!=:myTextColumn';
$stmt=db::db()->prepare($sql);
$stmt->execute($data);
if($stmt->fetchColumn()) {
$sql='UPDATE myTable SET myTextColumn=? WHERE id=?';
$stmt=db::db()->prepare($sql);
$stmt->execute($data);
return true;
}
else {return false;}
}
if(updateTable(array('id'=>123, 'myTextColumn'=>'Some text goes here')))
{
echo('It Changed');
}
1 个解决方案
#1
3
You can check the rowCount after your PDO call. It'll tell you the number of rows affected by your UPDATE.
您可以在PDO呼叫后检查rowCount。它会告诉你UPDATE影响的行数。
$sql='UPDATE myTable SET myTextColumn=? WHERE id=?';
$stmt=db::db()->prepare($sql);
$stmt->execute($data);
if ( $stmt->rowCount() > 0 )
{ /* do stuff */ }
更多相关文章
- 保存在Java桌面应用程序应用程序和网站上使用的个人用户设置的最
- MYSQL如何导出存储过程和触发器?
- 有没有办法确定是由Web应用程序(php进程)执行数据库行更新,还是通过
- mysql 触发器 自动补全字段
- 如何在产品和类别应用程序树中将1个表连接到(2个不同的表作为一个
- 在我的sql中调用触发器中的函数
- 求问vs窗体应用程序用gridview连接mysql未能获取数据库对象的列
- MYSQL在触发器中怎样实现‘根据条件来确定是否插入一条记录’?急!
- Android应用程序与外部数据库之间的安全性