I have this function, which takes some SQL and returns an HTML table:
<?php
function table_sql_result($sql) {
$str = "<table>";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
if (!$keys) {
$keys = array_keys($row);
$str .= "<tr>";
foreach ($keys as $key) {
$str .= "<th>$key</th>";
}
$str .= "</tr>";
}
$str .= "<tr>";
foreach ($row as $field) {
switch ($field) {
case NULL: $field = "NULL"; break; // This is catching empty strings, too!
case "": $field = " "; break;
default: $field = htmlentities($field); break;
}
$str .= "<td>$field</td>";
}
$str .= "</tr>";
}
return $str."</table>";
}
?>
Here’s the problem: I’m getting “NULL” for every empty value in the result. Considering the fact that PHP NULL is different than MySQL NULL, I’d like PHP to print “NULL” when it sees a field containing MySQL NULL, and " " when it sees a field with an empty string (“”). How would I do this?
My problem lies in that I can’t figure out how to detect MySQL NULL reliably. is_null() isn’t working.