Error Trying to get Property 'num_rows'

Asked

Viewed 555 times

1

I’m trying to log in, when sending the data to my method, this error appears:

Notice: Trying to get property 'num_rows' of non-object in \controller\user.class.php on line 37
Notice: Trying to get property 'mysqli_num_rows' of non-object in \controller\user.class.php on line 38
//LOGAR 
if (isset($_POST['btn-entrar'])) {
    $username = $_POST['email'];
    $password = $_POST['senha'];
    $password = md5($password);

    if (empty($username) || empty($password)) {
        echo "<div class='alert alert-info mb-2' role='alert'><strong>Atenção!</strong> Email ou Senha estão com os campos vazio, por favor, preencher.</div>";
    }else{
      $user = new User;

      if($user->getUser($username,$password)){
        session_start();
        //$_SESSION['logado'] = true;
        $_SESSION['usuario'] = $username;
        header("Location: ../index.php");
      }else{
        echo "<div class='alert alert-warning mb-2' role='alert'><strong>Atenção!</strong> Esse usuario não existe.
                </div>";
      }
    }
}
//CLASSE
<?php 
//includes
//include_once '../model/connection.php';
class Database{
    private $servername;
        private $user;
        private $password;
        private $dbname;

        public function __construct(){
            $this->servername = 'localhost';
            $this->user = 'root';
            $this->password = '';
            $this->dbname = '';
        }

        public function connect(){
            $conn = new mysqli($this->servername,$this->user,$this->password,$this->dbname);
            return $conn;
        }
}

    class User extends Database{

        public function getUser($username,$senha){
            $obj = new conectar();
            $conexao = $obj->conexao();

            $sql = "SELECT * FROM usuarios WHERE email = '$username' AND senha = '$senha'";




        $result = mysqli_query($conexao,$sql);
        //print_r($result);
        if (mysqli_num_rows($result)== 1) {
            return true;
        }
        return false;
        }
    }

WHERE I AM GOING WRONG?

  • You did: $result->num_rows. You checked what the value of $result? I mean, you read the method documentation query as to return?

  • opa , actually the if was the variable, but I was doing some tests and copied and put like this, but come on, I checked the value of $result,

  • I made some changes there,

  • following error appears, after some proper changes mysqli_num_rows() expects Parameter 1 to be mysqli_result, bool Given

  • And what has changed with these changes? What’s the new error? What’s the new result?

  • if you are using double quotes do not need to concatenate the variables with single quotes, the variables in php are read in double quotes, only one comment msm

  • If you have actually read the documentation, see that the function, in specific cases, returns true or false. You didn’t validate it in the code and the error is telling you exactly that: bool Given.

Show 2 more comments

1 answer

1


I think the problem might be this part

$numRows = $result->num_rows;
if ($result->mysqli_num_rows== 1) {
    return true;
}

According to the documentation the msqli::query can return false or MySQLi_Result, or be the first thing you should do and check whether the variable $result is not false, and also according to the documentation MySQLi_Result has no property by the name of mysqli_num_rows is a method with that name, then it must be called as follows mysqli_num_rows().

Applying the correction your code would be +/- like this:

if( $result ){
    // $result e um objeto 'MySQLi_Result'

    if ($result->mysqli_num_rows()== 1) {  // faltava o () `parenteses`
        return true;
    }

    /// nao entrou no if

}
return false;

mysqli::query

Value Returned


Returns FALSE on Failure. For Successful SELECT, SHOW, DESCRIBE or EXPLAIN darlings mysqli_query() will Return to mysqli_result Object.
For other Successful queries mysqli_query() will Return TRUE.

Which would be translated +/- like this:

Returns FALSE when you fail.
In the success of type queries SELECT, SHOW, DESCRIBE or EXPLAIN returns an object mysqli_result.
In the success of other types queries returns TRUE

The Mysqli class

  • So, I made these few changes, in the text, I’m re-electing the documentation of msqli::query, to see. pq presented a new error, Mysqli_result does not have a variable mysqli_num_rows(EXACTLY) so it was some crazy tests I was doing, finding that I missed something, inPo $result = mysqli_rows(query$connected,$sql); if (mysqli_num_rows($result)== 1) { Return true; } Return false;

  • 1

    Thank you very much, taught a lot and helped a lot, thank you very much, if I could give 10 soon, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.