2011-11-26 37 views
6

我想知道如何跳过从MySQL表中打印一行,如果变量为空。例如,我有一张充满信息的表格。我有一个回声响应结果。如果变量为空,我怎样才能跳过一个条目?如何在变量为空的情况下跳过while循环中的项目?

例如,如果'tweet1'在行中为空,我可以取消回显吗?

mysql_connect ($DBsever, $DBusername, $DBpass) or die ('I cannot connect to the database becasue: '.mysql_error()); 
mysql_select_db ("$DBname"); 

$query = mysql_query("SELECT * FROM $DBtable ORDER BY time"); 

while ($row = mysql_fetch_array($query)) { 
    echo "<br /><strong>".$row['time']." ".$row['headline']."</strong><br/>".$row['description']."<br />".$row['story1']." <a href=".$row['link1']." target='_blank'>".$row['link1']."</a> ".$row['tweet1']."<br />";} 
+1

使用条件和'continue'就移动到下一个迭代。 – thetaiko

回答

11

您可以使用跳过迭代continue控制结构。请阅读docs

例子:

if(!$row['tweet']) { 
    continue; 
} 
1

尝试:

while ($row = mysql_fetch_array($query)) { 
if ($row['tweet1']) 
    echo "<br /><strong>".$row['time']." ".$row['headline']."</strong><br/>".$row['description']."<br />".$row['story1']." <a href=".$row['link1']." target='_blank'>".$row['link1']."</a> ".$row['tweet1']."<br />"; 
} 
6
if (empty($row['tweet'])) { 
    continue; 
} 
6

您还没有信息tweet1返回行,这将使在tweet1不必要的数据php的检查。

$query = mysql_query("SELECT * FROM $DBtable WHERE tweet1 IS NOT NULL ORDER BY time"); 
0
while ($row = mysql_fetch_array($query)) { 
    if (!empty($row['tweet1']) { 
    echo "<br /><strong>".$row['time']." ".$row['headline']."</strong><br/>".$row['description']."<br />".$row['story1']." <a href=".$row['link1']." target='_blank'>".$row['link1']."</a> ".$row['tweet1']."<br />"; 
    } 
} 
相关问题