Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I skip an item in while loop if variable empty?

Tags:

sql

php

mysql

I'm trying to figure out how to skip printing a row from a MySQL table if the variable is empty. For instance, I have a table full of information. I have a while loop that echos the result. How can I skip an entry if a variable is empty?

For instance, can I cancel the echo if 'tweet1' is empty in the row?

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 />";}
like image 931
aspring Avatar asked Nov 26 '11 02:11

aspring


2 Answers

You can use continue control structure for skip an iteration. Please read the docs

Example:

if(!$row['tweet']) {
  continue;
}
like image 100
robert Avatar answered Nov 03 '22 15:11

robert


You could also not return rows without information in tweet1, this would make the php check for data in tweet1 unnecessary.

$query = mysql_query("SELECT * FROM $DBtable WHERE tweet1 IS NOT NULL ORDER BY time");
like image 6
Adam Wenger Avatar answered Nov 03 '22 15:11

Adam Wenger