Jump to content

Ignoring Null Table Fields


jmholl5

Recommended Posts

Let me start by saying I am very new to php and mysql. I am building a baseball team website and have a table that has class, playername, award1, award2, award3, award4, award5 as fields. The award fields are by default NULL. Each player wins a different amount of awards so I want to ignore fields that are NULL and I can't figure out how to do it. This is what I have right now.

 

<?

include("phpscripts/connect.php");

$query = "SELECT class,playername,award1,award2,award3,award4,award5 FROM playerrecognitions ORDER BY class DESC";

$result = mysql_query($query) or die(mysql_error());

 

while($row = mysql_fetch_array( $result )) {

echo "<h3>";

        echo $row['class'];

echo "</h3><h4>";

echo $row['playername'];

echo "</h4><ul>";

echo "<li>";

echo $row['award1'];

echo "</li>";

echo "<li>";

echo $row['award2'];

echo "</li>";

echo "<li>";

echo $row['award3'];

echo "</li>";

echo "<li>";

echo $row['award4'];

echo "</li>";

echo "<li>";

echo $row['award5'];

echo "</li>";

echo "</ul>";}

?>

 

I tried using an if statement but I couldn't make it work. Any help is greatly appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/150252-ignoring-null-table-fields/
Share on other sites

A better database design is having just three columns:  class, playername, award.

 

Then if you want multiple awards for a player, you add multiple rows to the table.  For example

 

Class  Playername  Award

1      John          Best drinker

1      John          Best sarcasm

2      Bob            Worst BO

 

Then you don't need to worry about nulls because they're not there.

 

If you really do want to keep your current database structure and detect nulls, you can do this:

if ($row['award4'] !== null) {
  echo $row['award4'];
  echo "</li>";
  echo "<li>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.