Null Values Handled Nicely with the PHP Error Suppression Operator

Posted on 29 April 2008 by LizzyFin

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Working on some database reports for a client’s membership roster and such, I came across a simple solution in handling some types of errors. Error messages filled a column in one report that should have output whether certain requirements were met by individual members of the organization.

Here’s the queries and partial output in question:

$fems = mysql_query(”SELECT `last_name`, `first_name`, MAX(`certs_ID`) AS mcrt, `fema_date`, `pin` FROM `members` LEFT OUTER JOIN `fema_attend` ON `pin` = `mem_pin` LEFT OUTER JOIN `fema` ON `fema_ID` = `fema_ids` LEFT OUTER JOIN `sar-certs` ON `member_pin` = `pin` LEFT OUTER JOIN `certs` ON `cert_ID` = `certs_ID` GROUP BY `last_name`, `first_name` “) or die (”Query failed here 12″);
while ($fem = mysql_fetch_array($fems)) {
echo “<td>$fem[last_name]</td>”.” <td>$fem[first_name]</td> “;

$high_cert = mysql_query(”SELECT `cert` FROM `certs` WHERE `cert_ID` = $certif[mcert]“);
while ($high_cer = mysql_fetch_array($high_cert)) {
echo “$high_cer[cert]“;
}…

And, here’s part of the ugly report that code section produced:

Ugly report.

What’s happening here to produce the error is that one of the queries produces some null values for the newest members of the organization who haven’t completed some requirements as of yet.

While fetching values from the $fems array an error is dumped on the screen that states:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\org_member.php on line 766

How did I figure out what that error meant? Using the tool phpmyadmin! Once you open phpMyAdmin and have selected your database, click on the SQL tab.

phpMyAdmin and its SQL tab.

I copied and pasted the first SELECT statement in the SQL box that is labeled “Run SQL query/queries on database psar-data:”, where psar-data is the name of my database.
SQL query inside phpMyAdmin.SQL query inside phpMyAdmin.

The page that is returned after pressing Go shows the SQL query and its output that contains NULL values.

phpMyAdmin and its SQL tab and query.

Now, when the data set was subjected to the second query the null values produced the invalid result resource warning error. The second query was this:

$high_crts = mysql_query(”SELECT `cert` FROM `certs` WHERE `cert_ID` = $fem[mcrt]“);
while ($high_crt = mysql_fetch_array($high_crts)) {
echo “$high_crt[cert]“;
}

Outputting values to the screen from this query would be so much nicer if the error would go away and just leave a blank. This example is a perfect use for the @ operator, or PHP error suppression operator. When placed in front of a PHP function the @ operator does not output error statements and mess up your data reports.

In the case where null values are encountered by the PHP function mysql_fetch_array the ugly warning error is produced. Placing the @ operator directly in front of mysql_fetch_array, like so @mysql_fetch_array, gives the output we’re seeking.

Output using the @ operator.

Yes, that output looks more professional without the error message and shows exactly what we want. In this case the blanks are meaningful and show exactly where a member needs to complete certain requirements.

Commenting Out Sections of Code Helps PHP Troubleshooting

Posted on 28 April 2008 by LizzyFin

Yesterday, at the end of a long day in front of this machine I could not for the life of me find the cause of this error:

"Parse error: syntax error, unexpected $end in"
my file on the last line. Now, I recognize that probably means I left out or hastily copied over a quote mark or left the semi-colon off the end of a php statement.

While 512 lines is no huge file, scanning those umpteen lines for a missing character was futile even though I looked at every line at least three times. Then it occurred to me to do what all good thinkers do…let my mind wander! It’s a trick that more people should know about, where you leave the problem at hand and free your mind of it for the time being. Miraculously, a solution will present itself. Michael, where ever you are, thanks for the tip!

Coming back to the issue this morning, I figured that I could “comment out” large portions of the code to isolate the problem. At first I started to comment out entire areas of the php by using <!–– ––> around the html portions that contained the php statements, but that didn’t work. Then, I realized that I should be commenting out portions of the php, so I used /* */ around smaller and smaller sections of the php code and finally found my error, a missing endif.

Comments come in handy for troubleshooting php!

Print Greener or Don’t Print at All

Posted on 1 April 2008 by LizzyFin

When you just have to print, use a free software program that checks your pages for wasteful junk before you hit print page.

GreenPrint is a small download that installs easily to help you reduce your printing costs. If we all print less, we can really impact the amount of energy used to destroy trees and make paper.

Check out these ecological facts about trees and paper.