Mysql错误:非对象的属性

时间:2014-11-19 19:39:31

标签: php mysql

function checkEmail($email){
  $sql = "SELECT email FROM users WHERE email='".$email."'";
  $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
  $result = $conn->query($sql);
  if ($result->num_rows > 0) {
    echo "prs";
  } else {
    echo "0 results";
  }   
}

我有这个功能。当我运行脚本时出现错误:注意:尝试在第21行的...中获取非对象的属性(if($ result-> num_rows> 0))。有什么问题?

1 个答案:

答案 0 :(得分:0)

或许查询结果为null 检查结果

if(isset($result) && !empty($result)){
  if ($result->num_rows > 0)
  { 
    echo "prs"; 
  }
 else
 { 
   echo "0 results"; 
  }
} 
else
 echo('0 results');