简体   繁体   中英

Why does this if statement of query return false?

The problem I face is that the conditional sentence which i typed return false!

if ($db->addkey($key_value,$key_balance,$key_created_date))
{
    echo 'TRUE';
}
else
{

    echo 'FALSE';

}

I need to say the $db->addkey($key_value,$key_balance,$key_created_date worked and pass the values to database as perfect but i'm still wired why this statement return false?

The addkey function is:

    public function addkey($key_value,$key_balance,$key_created_date){
    if ( !$this->link ) return false;
    $query = "INSERT INTO `" . $this->table_prefix . "keys` (`key_value`,`key_balance`,
        `key_created_date`)
        VALUES('" . $this->realEscapeSingle($key_value) .
        "', '" . $this->realEscapeSingle((int)$key_balance) . "', '" . $this->realEscapeSingle($key_created_date) . "')";

    ++$this->queries_;
    mysqli_query($this->link,$query);

    $key_id = mysqli_insert_id($this->link);
    if( !$key_id )
    {
        echo mysqli_errno($this->link) . ": " . mysqli_error($this->link);
        return false;
    }
}

And the Variables is:

$key_value = "25H8G-PIEAU-RB8H2";

$key_balance = 10;

$key_created_date = date("Y-m-d h:m:s");

You'll want to add a return true somewhere in the ->addkey method. Maybe at the bottom if no errors were detected?

Your problem is that you addkey method can never return true

Therefore when you test the method return in the if(...) statement, the if will never ever run, regardless of if the method performed its intended duty.

See the code:

public function addkey($key_value,$key_balance,$key_created_date){
    ....

  $key_id = mysqli_insert_id($this->link);
  if( !$key_id )
   {
     echo mysqli_errno($this->link) . ": " . mysqli_error($this->link);
     return false;
   }

 return true; // this catch that if none of the return false 
              // are triggered then the function MUST be true. 

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM