我设法创建了一个适合我的查询。但是现在我遇到了这样的问题:在给定当前显示结果的方式的情况下,查询将每个结果重复两次。我如何才能使其在一次显示的地方正确工作。

码:

$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");
}

}

最佳答案

代替使用mysql_fetch_array(),使用mysql_fetch_assoc()。默认情况下,mysql_fetch_array()将返回一个具有关联索引和数字索引的数组,这将导致您两次看到结果。

关于php - 如何防止该查询两次打印相同的结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10560114/

10-11 05:35
查看更多