Null Values Handled Nicely with the PHP Error Suppression Operator

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.

This entry was posted in mysql, php. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">