2012-05-12 59 views
0

我設法創建了一個適合我需求的查詢。但是現在我遇到了一些問題,考慮到當前顯示結果的方式,查詢重複每個結果兩次。我怎樣才能使它在一次顯示它的地方正常工作。如何防止此查詢打印兩次相同的結果?

代碼:

$sql = "SELECT DISTINCT contacts.contact_id, user_accounts.full_name, 
contact_notes.note_name, contact_notes.type, contact_notes.note_id, 
contact_posts.why_post, contact_posts.type, contact_posts.post_id 
FROM contacts, user_accounts, contact_notes, contact_posts 
WHERE (contacts.system_id = '$sid' AND contacts.contact_id = 
user_accounts.system_id AND contact_notes.user_allowed = '$everybody' AND 
contact_posts.user_allowed = '$everybody') OR (contacts.contact_id = '$sid' 
AND contacts.system_id = user_accounts.system_id AND contact_notes.user_allowed 
= '$everybody' AND contact_posts.user_allowed = '$everybody') 
LIMIT $startrow, 20"; 

$query = mysql_query($sql) or die ("Error: ".mysql_error()); 

$result = mysql_query($sql); 

if ($result == "") 
{ 
echo ""; 
} 
echo ""; 

$rows = mysql_num_rows($result); 

if($rows == 0) 
{ 
print(""); 

} 
elseif($rows > 0) 
{ 
while($row = mysql_fetch_array($query)) 
{ 

$noteid = htmlspecialchars($row['note_id']); 
$note_title = htmlspecialchars($row['note_name']); 
$postid = htmlspecialchars($row['post_id']); 
$postreason = htmlspecialchars($row['why_post']); 
$datetimeadded = htmlspecialchars($row['just_date']); 

print("<br /> <br />$note_title - $noteid <br /><br />$postreason - $postid"); 
} 

} 

回答

2

而不是使用mysql_fetch_array()使用mysql_fetch_assoc()的。默認情況下,mysql_fetch_array()將返回一個包含關聯數字索引的數組,這將導致您看到兩次結果。

+0

嘿,那仍然重複兩次查詢。 – ariel

+0

嘿,我仍然需要一些幫助。與mysql_fetch_assoc()我得到同樣的問題。 – ariel