doubeng9567 2015-08-19 15:23
浏览 47
已采纳

查询结果为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 2015-08-19 15:28
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow