1

Consider the following simple PHP code:

<?php  
   $db=new mysqli('localhost', 'root','','apeirosto');            
   $query="UPDATE REQUESTS SET STATUS=1 where requestid=155"; 
   $result=$db->query($query);  
   if (!($result)) {
     $message= "fail_update";   
     echo $message;
   }
   else
   {
     $message= "success";   
     echo $message;
   }  
?>

My secondary question:

 $result=$db->query($query);

plays the role of commit in DB?

My main question:

even if there is no row with PK requestid=155, $result returns true and the message I get is "success"... Why? How may I get an error message in such cases?

Thank you

Unknown developer
  • 5,414
  • 13
  • 52
  • 100
  • check for errors on the query http://php.net/manual/en/mysqli.error.php because this `$message= "fail_update"; echo $message;` doesn't help you at all. – Funk Forty Niner Aug 19 '15 at 15:29
  • *"even if there is no row with PK requestid=155"* - MySQL will NOT add it to your db. You need to use INSERT. – Funk Forty Niner Aug 19 '15 at 15:32
  • I the answer was useful for you, please mark it as accepted in order to help future visitors. – Ahmad Sep 10 '15 at 17:49

1 Answers1

3

The query method returns false only when there is an error in executing. When no changes happens, there is no error, so query returns something other than false.

You can use affected_rows property to check any rows are updated. It returns number of changed rows after the query.

More information: http://php.net/manual/en/mysqli.affected-rows.php

Ahmad
  • 5,551
  • 8
  • 41
  • 57