2013-05-18 31 views
0

我正在php中製作一個類似的系統,我有一個代碼來獲取所有喜歡使用帖子ID的人的用戶名,如果他們不喜歡這篇文章,它會顯示一個按鈕,說像是帖子,如果他們沒有,它會顯示一個按鈕,說不像帖子,如果沒有像它會說「沒有人喜歡這個帖子是第一個」。但是,當我將其添加在,它只能獲得1用戶名,即使多個人都喜歡的崗位,我怎麼解決了這個問題,這裏是我的代碼:SQL只能得到1行

$fancy = $db->fetch("SELECT * FROM " . $prefix . "_fancy WHERE post_id = '" . $post_row['id'] . "' ORDER BY id"); 

if ($fancy) { 
    $name = $user->name($fancy['account_id']); 
    if ($account['id'] !== $fancy['account_id']) { 
     $fancytext = '<div>'.$name.' Like this post.<!-- BEGIN logged_in --> <a href="./?area=forum&amp;s=topic&amp;t='.$topic_id.'&amp;f='.$pid.'"><img src="./template/default/images/like.png" alt="" border="0"/></a><!-- END logged_in --> 
</div>'; 
    } else { 
     $fancytext = '<div>'.$name.' Like this post.<!-- BEGIN logged_in --> <a href="./?area=forum&amp;s=topic&amp;t='.$topic_id.'&amp;unf='.$pid.'"><img src="./template/default/images/unlike.png" alt="" border="0"/></a><!-- END logged_in --> 
</div>'; 
    } 
} else { 
    $fancytext = '<div><i>No one has liked this post, be the first!</i> <!-- BEGIN logged_in --><a href="./?area=forum&amp;s=topic&amp;t='.$topic_id.'&amp;f='.$pid.'"><img src="./template/default/images/like.png" alt="" border="0"/></a><!-- END logged_in --> 
</div>'; 
} 
+0

請至少描述一下表格嗎?謝謝 –

+2

擺脫所有的造型;它不相關,使你的代碼變得龐大。 – imulsion

+0

你在查詢中有一個WHERE。這可能是你的問題。數組中是否有多個'$ post_row ['id']'?你有沒有迴應該數組? – ntgCleaner

回答

0

釋放出功率PDO的預處理語句和fetchAll

$sql = "SELECT * FROM $prefix_fancy WHERE post_id = :pid ORDER BY id"; 
$stmt = $db->prepare($sql); 
$stmt->execute(array(':pid' => $post_row['id'])); 
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC); 

現在$posts握着你的所有選定的記錄。使用foreach進行迭代。

+0

致命錯誤:調用未定義的方法db :: prepare()在/ homepages/43/d456722778/htdocs/forum/core/line 608上的topic.php – Malik

+0

@Malik:請在你定義$ db的地方發佈你的代碼。 – michi