如果变量为空,我正试图弄清楚如何跳过从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 />";}
答案 0 :(得分:13)
答案 1 :(得分:6)
if (empty($row['tweet'])) {
continue;
}
答案 2 :(得分:6)
你也可能无法返回tweet1
中没有信息的行,这会使php检查tweet1中的数据变得不必要。
$query = mysql_query("SELECT * FROM $DBtable WHERE tweet1 IS NOT NULL ORDER BY time");
答案 3 :(得分: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 />";
}
答案 4 :(得分: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 />";
}
}