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 に答える