PHP变量看起来是空的,但实际上不是


PHP variable appears to be null while it is not

我有一个小的php类,我已经编辑了一点问我的问题。类如下所示

class Register
{
    public $notification = null;
    public function __construct()
   {
       create_connection();
      $this->validate_register();
    }
    public function validate_register()
    {
            //edit: missing double quote close
        $select_register = "SELECT * FROM `student_reg`";
        if($select_register_run = mysql_query($select_register))
        {
            $rows_returned = mysql_num_rows($select_register_run);
            if($rows_returned >= 1)
            {
                $this->notification = 'error';
            }else if($rows_returned == 0){
                $this->notification = 'success';
            }
        }else{
            $this->notification = 'error';
        }
        if($this->notification != null)
        {
            echo 'not null';
        }else{ echo 'null';}
    }
}
$new_register = new Register();
?>

很明显,从类中,在任何可能的级别上,都有一个值赋给$this->notification。但是由于某些原因,类'回显' null。

我构建的creat_connection()函数工作完美,但我已经为这个问题的目的提交了它。

为什么会这样?

实际上,如果$rows_returned小于1且不等于0,代码将回显'null',所以我建议您回显$rows_returned

试试这样…

$select_register_run = mysql_query($select_register);
if($select_register_run){
//rest code
不是

if($select_register_run = mysql_query($select_register))