0

Let Suppose i have Following code which updates two different table

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){
        ....
    }

}

So is it possible to check whether first query is updated any row or not ?

TarangP
  • 2,711
  • 5
  • 20
  • 41
  • $sql = "sql statement"; mysql_query($sql)or die(mysql_error()); if(mysql_affected_rows() || mysql_affected_rows() == 0){ // no error }else{ // error } – sunny bhadania Apr 10 '18 at 06:34

1 Answers1

0

IN 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.

so,

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

you have to integrate this in php as it is purely mysql syntax

Kedar Limaye
  • 1,041
  • 8
  • 15