PruitIgoe
PruitIgoe

Reputation: 6384

php: is_null and database results

I'm having a problem trapping a database result where the value is null. I've tried different angles from (this is all in the

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

loop):

if(is_null($thisdept) == true) {
if(!is_numeric($thisdept) { 
if($thisdept == "" || $thisdept == NULL || $thisdept == "null" || $thisdept == "NULL") { 

no luck. I can't even seem to log it:

$isNull = is_null($thisdept);
$firephp->fb($isNull . "::" . $thisid);

doesn't even write to the console (yes, firephp is included correctly and works).

Here's the entry in the db (mysql) - item 19, column 3

enter image description here

I'm sure there's operator error going on here but I just can't seem to figure out what I am doing wrong. Any help would be appreciated.

More code:

if ($itemcount > 0) {

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

            //clean up data
            $thisid = $row['id'];
            $thisdate = $row['theDate'];
            $thisdept = $row['department'];
            $thisbucket = $row['bucket'];
            $thispub = $row['publication'];
            $thisarea = $row['area'];
            $thishours = $row['hours'];
            $thisdesc = $row['description'];
            $thistimestamp = $row['theTimestamp'];
            $thissortdate = $row['sortdate'];
            $workDate = $row['workDate'];


            $isNull = is_null($thisdept);
            $firephp->fb($isNull . "::" . $thisid);
            //if department == null then we should just select the user's department and they can fix it on an edit
            //$thisdept == "" || $thisdept == "null" || $thisdept == "NULL" || $thisdept == null || $thisdept = "Null"
            if (is_null($thisdept) == true) {

                $udQuery = "SELECT department FROM users WHERE username = '" . $username . "'"; 
                $udResults = $db->getResults($udQuery);
                $udItemCount = count($udResults); 
                if ($udItemCount > 0) { 
                    while ($udRow = mysql_fetch_array($udResults)) { 
                        $thisdept = $udRow['department'];
                    }
                }
            }

Upvotes: 1

Views: 432

Answers (1)

OTN
OTN

Reputation: 233

Where does $thisdept come from? You are fetching the MySQL result to $row, than you have also to use that array!

Checking if the variable is null, you can do that with is_null(), that's sufficient, if it is null, than it will give the boolean TRUE.

See also:

http://www.php.net/manual/en/language.types.null.php

http://www.php.net/manual/en/function.is-null.php

EDIT:

Try this:

if (is_null($thisdept) == true) {

after this if statement, try to echo something to look if the code reaches till there if so, maybe there is something wrong with your $username?

Upvotes: 1

Related Questions