always-a-learner always-a-learner -4 years ago 97
MySQL Question

How to get different error message using elesif

I am working on one Login system which is send a verification link to the user and user have to verify within the one hour.

So When user click on verification link. I get user id and the code form url.

So by using this First i Will check is it correct or not form database. This is the query.

$query = $this->db
->select('id,expiry_time')
->where('id', $id)
->where('code', $code)
->get('candidate');
$data = $query->row_array();


So I have to check if user has excluded the expiry_time limit or not so i use elseif.

if ($data === null) {
echo "Verification code not correct";
} elseif ($data['expiry_time'] < $expiry_time) {
echo "Your verification Time is expire ";
} else {
echo "Code is correct & Time is Not Expire yet";
}


So here i am facing a problem. I did not get the proper message.

I want Separate Message if code is not correct & if time is expire.
I am little confused about it so if you suggest any correct way of getting proper error message than please tell me.
Thanks.

Answer Source

Your ElesIf Loop Looks Correct. I Think You Need to Understand it first.

so this first if the condition

if ($data === null) {
  echo "Verification code not correct";
}   

Here I know why you use Null but you can do if with the empty() also. So if your user code is not correct then you can return this message to the view.

if (empty($data) {
   return $code_incorrect; //Your Code incorrect message
} 

Now If code is correct but time is expire then the elseif gives you the error message regarding this.

elseif ($data['expiry_time'] < $expiry_time) {
    return $time_expire;  // Your Time Expire message for user.
}

If code is correct and time expire you get error message of expiry and if code is incorrect then you get the error message for code incorrect.

now then else statement

else {
  return $sucess; //sucess message thet code is correct and time is not expire yet
 }

Here you can get the success message if both the condition is true.

This is the syntax of elseif

   if (condition) {
     code to be executed if this condition is **true**;
   } elseif (condition) {
     code to be executed if this condition is **true**;
   } else {
      code to be executed if all conditions are **false**;
   }

I hope You Understand it properly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download