問題描述
我試圖在刪除某行之前檢查它是否存在.我的表中的行不存在,但它總是返回 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!