Trying to get the property 'num_rows'

Asked

Viewed 92 times

0

I am not able to solve this error, I am more than 5 hours programming kkk, I missed something ?

here is my code(THIS IN COMMENT WHERE HE SAYS THAT IS THE MISTAKE)

I am doing validation to log in

Trying to get Property 'num_rows' of non-object in

<?php 
//includes


	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 = $this->connect()->query($sql);

			
      
      //AQUI ESTA O ERROR
      $numRows = $result->num_rows;
			
      
      if ($numRows == 1) {
				return true;
			}
			return false;
		}
	}

  • Try passing $result->num_rows straight into if: if ($result->num_rows == 1) instead of putting it into a variable; If that’s the case you can check what’s coming into it with a print_r. Just so you know if you’re passing any information.

  • tried, and nothing,

  • I used the print_r, and it is passing the information

1 answer

0

To log in using PDO I do not use Count Rows, I use the fetchAll() and check the amount of results, thus:

//SQL Statement
$query = $conn->prepare("SELECT * FROM cadUsuario WHERE email=:email AND senha=:senha");
$query->bindParam(":email", $_POST['email']);
$query->bindParam(":senha", $_POST['senha']);

//Executa a query
$query->execute();

//Retorna os resultados
$usuario = $query->fetchAll();

//Se mais de um resultado, executa o código para login
    if($usuario >0) {
    //código vai aqui
}

According to the PHP manual, fetchAll():

Pdostatement::fetchAll - Returns an array containing all lines of outworking

  • then I’ll try to adjust according to my code,

  • Nothing, has not been solved

Browser other questions tagged

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