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 documentationqueryas to return?– Woss
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,
– Vitor
I made some changes there,
– Vitor
following error appears, after some proper changes mysqli_num_rows() expects Parameter 1 to be mysqli_result, bool Given
– Vitor
And what has changed with these changes? What’s the new error? What’s the new result?
– Woss
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
– Martins Luan
If you have actually read the documentation, see that the function, in specific cases, returns
trueorfalse. You didn’t validate it in the code and the error is telling you exactly that: bool Given.– Woss