php
2013-08-18 127 views -1 likes 
-1

當從fetch_assoc回顯結果時,我希望跳過空(NULL)結果。 現在我的代碼是:php fetch_assoc跳過空結果

while ($rowsoft = $resultsoft->fetch_assoc()) { 
echo "<table class='swtable' cellspacing='0' summary='Software table'> 
<tbody> 
<tr><td class='software_up'><a class='sw' href='software.php?id=" . $rowsoft['id'] . "'>" . $rowsoft['title'] . "</a></td></tr> 
<tr><td class='software'><div class='tags'>Tags: </div><a class='tags' href='#'>" . $rowsoft['tag_1'] . "</a> 
<a class='tags' href='#'>" . $rowsoft['tag_2'] . "</a><a class='tags' href='#'>" . $rowsoft['tag_3'] . "</a></td></tr> 
} 

但並不總是存在於$結果rowsoft [ 'TAG_1']或$ rowsoft [ 'TAG_2']或$ rowsoft [ 'tag_3']。 如何更改fetch_assoc()內部的回顯以使其僅在存在結果時才起作用? 或者我應該使用foreach還是其他的東西?

謝謝

+6

爲什麼不在SQL查詢中排除它們 – Musa

+0

如果你解決了這個問題,請不要在問題的標題上附加*「solve」*,但考慮[接受一個答案](http:// meta .stackexchange.com/a/5235/224130)。 –

回答

0

我把它通過改變SQL:

select 
...  
    (select concat('<a href=\'#\' class=\'tags\'>',t.tag_bg,'</a>') from tags t where t.id=s.tag1) tag_1, 
    (select concat('<a href=\'#\' class=\'tags\'>',t.tag_bg,'</a>') from tags t where t.id=s.tag2) tag_2, 
    (select concat('<a href=\'#\' class=\'tags\'>',t.tag_bg,'</a>') from tags t where t.id=s.tag3) tag_3 
from software where... 

,然後FETCH_ASSOC()內:

<tr><td class='software'><div class='tags'>Tags:</div>" . $rowsoft['tag_1'] . "" . $rowsoft['tag_2'] . "" . $rowsoft['tag_3'] . "</td></tr> 

的建議偉大的作品,謝謝!

0

您可以按照@Musa的說法將它們排除在查詢之外。這可能是DB做這項工作的最好方式。你也可以使用一堆if語句來測試$ rowsoft ['tag_1']是否爲null,然後不執行回聲。

相關問題