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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效