2

我使用这种语法而不是 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} 

但它似乎没有返回正确的东西,知道吗?

4

2 回答 2

5

这里的使用EXISTS似乎是错误的。只需执行此查询:

SELECT 1 
FROM TheCommunityReachLinkingTable 
WHERE communityKey = :communityKey 
AND userID = :userID
于 2012-06-04T14:49:11.643 回答
0

正确的用法和平常一样,捕获 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
}
于 2012-06-04T16:03:31.047 回答