mysqli_num_rows() expects Parameter 1 to be mysqli_result, bool

Asked

Viewed 616 times

0

I have an error when it comes to authenticating in PHP 7. This code works in PHP 5. I made the changes to msql for msqli, but the problem continues. Someone can give a strength?

<?php

include "conexao.php";
$con = conectar();

$username = $_POST["username"];
$senha    = $_POST["senha"];



$resultado = mysqli_query($con,"SELECT * FROM usuario where username='$username'");
$linhas = mysqli_num_rows($resultado); // erro nesta linha

if ($linhas==0)     {


echo "<html><body>";
echo "<p align= \"center\"> Usuário não encontrado ! </p>";
echo "<p align= \"center\"> <a href = \"login.html\"> Voltar </a> </p>";
echo "</body></html>";

} else {

if ($senha != mysqli_result($resultado, 0, "senha")) {              

echo "<html><body>";
echo "<p align= \"center\"> A senha está incorreta ! </p>";
echo "<p align= \"center\"> <a href = \"login.html\"> Voltar </a> </p>";
echo "</body></html>";

} else          {        

header ("Location: pagina_inicial.php");

}

}               

mysqli_close($con);
?>

1 answer

1

The problem is that there is an error in your query. According to PHP documentation, when using mysqli_query:

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

That’s why you’re passing a Boolean to $resultado. Try changing your query to: "SELECT * FROM usuario where username=$username".

Browser other questions tagged

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