尝试使用if函数获取非对象的属性

时间:2015-01-17 19:47:16

标签: php

错误:

  

注意:尝试在第16行的C:\ Users \ Gustavs \ Documents \ EasyPHP \ test2 \ register.php中获取非对象的属性
  注意:尝试在第20行的C:\ Users \ Gustavs \ Documents \ EasyPHP \ test2 \ register.php中获取非对象的属性   代码:

$exists = '';
$result = $connect->query('SELECT username FROM users WHERE username = '.$username.' LIMIT 1');
if ($result->num_rows == 1) {
    $exists = 'u';
}   
$result = $connect->query('SELECT email from users WHERE email = '.$email.' LIMIT 1');
if ($result->num_rows == 1) {
    $exists = 'e';
}

16行:

if ($result->num_rows == 1) {

20行:

if ($result->num_rows == 1) {

1 个答案:

答案 0 :(得分:0)

试试这个:

    $exists = '';
    if($result = $connect->query('SELECT username FROM users WHERE username = "'.$username.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'u';
        }else{
            die;
        }
    }   
    if($result = $connect->query('SELECT email FROM users WHERE email = "'.$email.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'e';
        }else{
           die;
        }
    }

或者,根据您如何检查查询是否成功,您可以执行以下操作:

    $exists = '';
    if($result = $connect->query('SELECT username FROM users WHERE username = "'.$username.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'u';
        }
        return true;
    }else{
        return false;
    }   
    if($result = $connect->query('SELECT email FROM users WHERE email = "'.$email.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'e';
        }
        return true;
    }else{
        return false;
    }

甚至:

    $exists = '';
    if($result = $connect->query('SELECT username FROM users WHERE username = "'.$username.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'u';
        }
        echo 'Success!';
    }else{
        echo 'Failure!';
    }   
    if($result = $connect->query('SELECT email FROM users WHERE email = "'.$email.'" LIMIT 1')){
        if($result->num_rows == 1){
        $exists = 'e';
        }
        echo 'Success!';
   }else{
        echo'Failure';
    }

我希望这有帮助!