Jump to content

using empty


jimfog

Recommended Posts

I am trying to use empty() to see if the result row coming from the db is empty or not.Take a look at this code first:

$expirystatus=$result->fetch_object();           $expire=$expirystatus->nonce;           if(!empty($expire))           {$expire='non-expired';}           else           $expire='expired';

The above code checks if a nonce in the has expired or not.The query that gets the nonce is this:

select nonce from email_change where now()-interval 1 day<nonce_time_creation                and nonce="'.$nonce.'"'

In other words if the nonce created within the last day, the nonce will be returned and $expire will be set to 'non-expired'.

Otherwise(when the nonce is older that one day) what will be returned is an empty result(empty row).

 

The problem appears in this 2nd case. I get the usual error trying to get a property of a non-object.

Why that happens? And the curious thing is that the code does work.

 

$expire is set to 'expired'.

Here is the whole function:

function  check_expiry_status($connection,$nonce)                {     $connection->set_charset("utf8");     $result = $connection->query('select nonce from email_change where now()-interval 1 hour<nonce_time_creation                and nonce="'.$nonce.'"');     if(!$result)         {            echo 'problem';            return false;         }     else{$expirystatus=$result->fetch_object();           $expire=$expirystatus->nonce;           if(!empty($expire))           {$expire='non-expired';}           else           $expire='expired';                  }                      return  $expire;}
Link to comment
Share on other sites

 

Because the query didn't return any results.

 

Ok, what can I do here? As I said if the nonce is expired no result will be returned by definition.

Is there a workaround?

What conditional can I write that test if none result is returned...

 

if(none result returned)

{nonce is expired}

Link to comment
Share on other sites

If you have this line:

 

$expirystatus=$result->fetch_object();

 

and you are expecting that fetch_object might return null, then why would your next line be this:

 

$expire=$expirystatus->nonce;

 

If you think that $expirystatus might be null, then why are you trying to get a property of it instead of checking if it is null?

Link to comment
Share on other sites

If you have this line:

 

$expirystatus=$result->fetch_object();

 

and you are expecting that fetch_object might return null, then why would your next line be this:

 

$expire=$expirystatus->nonce;

 

If you think that $expirystatus might be null, then why are you trying to get a property of it instead of checking if it is null?

 

I tried this code:

 else{$expirystatus=$result->fetch_object();        if($expirystatus===null)        {$expire='expired';}         else        {$expire='non-expired';}     var_dump($expirystatus);      }

and I saw that expirystatus is NULL either way...nonce expired or not.

 

Any suggestions?

Link to comment
Share on other sites

You are right...I had made a silly mistake :facepalm: , when I was calling the function was with a different nonce from the one found in the db.

 

Sorry...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...