php
2013-08-18 47 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,然后不执行回声。

相关问题