PDO查询总是返回1或true [英] PDO query is always returning 1 or true

查看:91
本文介绍了PDO查询总是返回1或true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在删除行之前检查行是否存在.我表中的行不存在,但总是返回1:

I am trying to check if a row exists before I delete it. The row in my table doesn't exist but it always returns 1:

$orders = $this->db->prepare("SELECT * FROM orders WHERE id=? AND user=?"); 
            $check = $orders->execute(array($message,$this->model->checkapi($data,$message)));
            echo $check;
            if($check){
            $deleteorder = $this->db->prepare("DELETE FROM orders WHERE id=? AND user=?"); 
            $deleteorder->execute(array($message,$this->model->checkapi($data,$message)));
                array_push($result, array('success' => true,
                                          'deleted' => $message));
                echo json_encode(array("result" => $result));
                die();
            }else{

$this->model->checkapi($data,$message) 返回假用户名,id/$ message返回136

$this->model->checkapi($data,$message) returns fakeusername and id/$message returns 136

我已经检查了我的数据库,该ID存在,但ID和用户名却不存在.

I've checked my database, the ID exists, but not the id and username together.

我正在发送ID:136和用户名:fakeuser

I'm sending id: 136 and username: fakeuser

在数据库中,该行以id:136和用户名:demo存在.

in the databse the row exists as id:136 and username: demo.

我不确定为什么由于行不匹配而无法选择行时为什么返回1.

I'm not sure why it's returning 1 when the row shouldn't be selected due to it not matching.

推荐答案

您应该在execute()之后使用fetch(),因为execute()成功返回TRUE或失败返回FALSE:

You should use fetch() after execute(), because execute() just returns TRUE on success or FALSE on failure :

$orders->execute(...
$result = $orders->fetch(PDO::FETCH_ASSOC);
print_r($result);

这篇关于PDO查询总是返回1或true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆