[英]MySQL & php PDO how to fetch: SELECT EXISTS (SELECT 1 FROM x WHERE y = :value)
我使用這種語法而不是count(*),因為它應該更快,但我不知道如何獲取結果輸出
$alreadyMember = $dataBase->prepare('SELECT EXISTS ( SELECT 1
FROM TheCommunityReachLinkingTable
WHERE communityKey = :communityKey
AND userID = :userID)');
$alreadyMember->bindParam(':communityKey', $_POST['communityKey'], PDO::PARAM_STR);
$alreadyMember->bindParam(':userID', $_POST['userID'], PDO::PARAM_INT);
$alreadyMember->execute();
if($alreadyMember->fetch()) {do code here}
但這似乎並沒有返回正確的東西,有什么想法嗎?
在這里使用EXISTS
似乎是錯誤的。 只需執行以下查詢即可:
SELECT 1
FROM TheCommunityReachLinkingTable
WHERE communityKey = :communityKey
AND userID = :userID
正確用法與通常一樣,捕獲fetch()的返回值
$row = $alreadyMember->fetch();
print_r($row); // you may have numeric or string indices depending on the FETCH_MODE you set, pick one, consider a column alias and associative
//or the easy way
$alreadyMember->execute();
if ($alreadyMember-fetchColumn()) {
//column 0 contained a truthy value
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.