doubeng9567
doubeng9567
2015-08-19 15:23

查询结果为true而不是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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqt20140129 dqt20140129 6年前

    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

    点赞 评论 复制链接分享