dongxin0031 2014-08-05 15:07
浏览 207
已采纳

php mysqli受影响的行在删除语句后返回错误的结果

I have a function to delete a row from my table and I like to return true if the row existed before. I use the following function achieve that:

$mysqli = $this->mysqli;

$stmt = $mysqli->prepare("DELETE FROM active_clients WHERE token LIKE ?");
$stmt->bind_param('s', $token);
$stmt->execute();
if($stmt->affected_rows < 1){
    $stmt->close();
    return false;
}
$stmt->close();
return true;

It also worked propperly on my local host but when I moved to live server it doesn't return the correct result. affected_rows returns always 0 but the row is deleted as it should be. How can it be? Could there be a problem with php-version? I run locally 5.4 and on live server 5.3... Or ist it also possible that it has something to do with database or server configuration?

  • 写回答

2条回答 默认 最新

  • douchun1961 2014-08-05 15:47
    关注

    Try this:

    $mysqli = $this->mysqli;
    
    $stmt = $this->mysqli->prepare("DELETE FROM active_clients WHERE token LIKE ?");
    if(!$stmt){
        die('prepare() failed: ' . $mysqli->error);
    }else{
        $stmt->bind_param('s', $token);
    
        if($stmt->execute()){
           $stmt->store_result();
            if($stmt->affected_rows < 0){
                $stmt->close();
                return false;
            }else{
                $stmt->close();
                return true;    
            }
        }else{
            die('execute() failed: ' . $mysqli->error);
        }
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler