简体   繁体   中英

Query result is true instead of false

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM