10

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 />";}
1
  • 1
    use a condition and a continue to move on to the next iteration. Commented Nov 26, 2011 at 2:49

5 Answers 5

15

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

Example:

if(!$row['tweet']) {
  continue;
}
Sign up to request clarification or add additional context in comments.

Comments

6

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");

Comments

6
if (empty($row['tweet'])) {
    continue;
}

Comments

1

Try:

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 />";
}

Comments

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 />";
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.