php - 检查多个查询中的特定查询是否受到影响

标签 php mysql sql sql-update rows-affected

假设我有以下代码来更新两个不同的表

if(isset($_POST['submit'])){

    $updateq = $conn->query("UPDATE `tbl1` SET `field1`= '$field1'");

    $updater = $conn->query("UPDATE `tbl2` SET field2 ='$field2'");

    //Here I want to check if first update query
    //is affected any row or not
    if(mysqli_affected_rows($conn) > 0){
        ....
    }

}

那么是否可以检查第一个查询是否更新了任何行?

最佳答案

在 mysql

  • ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT. For other statements, the value may not be meaningful.

所以,

UPDATE `tbl1` SET `field1`= '$field1'
SET @countRow =  ROW_COUNT();
if (@countRow>0) then
UPDATE `tbl2` SET field2 ='$field2'
end if;

你必须将其集成到 php 中,因为它纯粹是 mysql 语法

关于php - 检查多个查询中的特定查询是否受到影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49746821/

相关文章:

php - MySQL 查询 : generate a report of the top votes for the each food type

python - Twisted + SQLAlchemy 和最好的方法

PHP - 如何获取没有值的 $_GET 参数

php - 阿拉伯语不存储在 mysql/php 中

php - 在 PHP 中,当我使用 PDO 调用 MySQL 存储过程,然后进行另一个查询时,出现错误 :

php - 如何使用 php 使用户名条目必须是唯一的

php - 如何在 php 中从 mysql 数据库生成图形和图表

PHP SQL - 无法找到用户是否存在

php - Android通知推送php错误

php - 第 5 行 : Call to undefined function link_to_route()