PDO query is always returning 1 or true(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)返回 fakeusername 和 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PDO 查询总是返回 1 或 true
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
