Mysqli + num_rows after a table query

Asked

Viewed 349 times

1

This is supposed to be a login system with access levels that I’m trying to do, but it’s giving an error.

What am I missing?

I’ve tried everything and nothing. I’ve looked at the PHP documentation and nothing.

I wanted it to be object-oriented Mysqli, not PDO.

<?php

// inclusao do arquivo de conexao ao banco de dados
include("conexao.php");

// se existir o clicar do botao logar do formulario
if(isset($_POST['logar'])){

    // se existir o login do usuario digitado
    if(isset($_POST['loginUsuario'])){

        // verificação de anti injeção de SQL do campo login do usuario
        $loginUsuario = $conexao->escape_string($_POST['loginUsuario']);

        //se existir a senha do usuario digitado
        if(isset($_POST['senhaUsuario'])){

            // verificação de anti injeção de SQL do campo senha do usuario
            $senhaUsuario = $conexao->escape_string($_POST['senhaUsuario']);

            $consulta = "SELECT * FROM usuarios WHERE loginUsuario = $loginUsuario AND senhaUsuario = $senhaUsuario";
            $resultado = $conexao->query($consulta);
            $linha = $conexao->num_rows($resultado);

            echo $linha;// Aqui queria que dizesse que existe o registro pelo login do usuario
                        // Eu digitasse login: admin e senha: 123, aparecesse 1, porque tem dentro do registro
                        // E se digitasse qualquer outra coisa aparecesse 0, porque não tem dentro do registro

        }// isset do campo senha do usuario
    }// isset do campo login do usuario
}//isset botão logar

?>
  • Error appears?

  • Which error appears?

  • Error 500, I am making this application inside my hosted server.

  • https://weec-sistemas.000webhostapp.com/login.php puts something in the login to see the error.

  • You need to enable the error view. Add these two lines at the beginning of the script. ini_set('display_errors', true); error_reporting(E_ALL);

  • I’ll put it on now, let’s see.

  • thanks for the tip.

  • gave this error now Fatal error: Call to Undefined method mysqli::num_rows() in /Storage/ssd3/459/3838459/public_html/login.php on line 24

  • already changed, but the same error. my script is above what I’m missing?

  • This is login.php, but everything is inside a page.

  • Below the last line of the code you have a include to call the header.php page and a form with login and button fields, then another include with footer.php and a mysqli_close();

Show 6 more comments

3 answers

2


<?php
    ini_set('display_errors', true); error_reporting(E_ALL);

    // inclusao do arquivo de conexao ao banco de dados
    include("conexao.php");

    // se existir o clicar do botao logar do formulario
    if(isset($_POST['logar'])){

        // se existir o login do usuario digitado
        if(isset($_POST['loginUsuario'])){

            // verificação de anti injeção de SQL do campo login do usuario
            $loginUsuario = $conexao->escape_string($_POST['loginUsuario']);

            //se existir a senha do usuario digitado
            if(isset($_POST['senhaUsuario'])){

                // verificação de anti injeção de SQL do campo senha do usuario
                $senhaUsuario = $conexao->escape_string($_POST['senhaUsuario']);

                //************** correção foi aqui ************************* 
                $consulta = $conexao->query("SELECT * FROM usuarios WHERE loginUsuario='$loginUsuario' AND senhaUsuario = '$senhaUsuario'");
                $linha = $consulta->num_rows;

                echo $linha;

            }// isset do campo senha do usuario
        }// isset do campo login do usuario
    }//isset botão logar

    // inclusão do arquivo cabeçario da pagina HTML5
    include("header.php");    
?>
 <center>
    <h1>Entre para logar</h1>
 <!-- formulario de login e senha do usuario -->
 <form action="login.php" method="post">
    <input type="text" name="loginUsuario" maxlength="15" size="15" placeholder="Usuario">
    <input type="password" name="senhaUsuario" maxlength="15" size="15" placeholder="********">
    <input type="submit" name="logar" value="Logar">    
 </form>
</center>

<?php
    // inclusão do arquivo rodape do HTML5
    include("footer.php");
    //fechar banco de dados 
    $conexao->close();
 ?>

NOTE: Surround query variables with single quotes

WHERE login='$loginUsuario' AND senha = '$senha'"

num_rows - Get the number of lines in a result

First case

$consulta = $conexao->query("SELECT * FROM usuarios WHERE loginUsuario='$loginUsuario' AND senhaUsuario = '$senhaUsuario'");
//numero de linhas afetadas
$linha = $consulta->num_rows;

or

$linha = mysqli_num_rows($consulta);

Second case

as the response of [rray] rray and involving the query variables with single quotes.

$consulta = "SELECT * FROM usuarios WHERE loginUsuario='$loginUsuario' AND senhaUsuario = '$senhaUsuario'";
$resultado = $conexao->query($consulta);
$linha = $resultado->num_rows;
  • @Wesleyrodrigues looks there in his answer edited by me. I put the code of your link with the suggested correction throughout the comments

  • I copied your code and put it on the website but still error, is it not the hosting? because its works and mine does not

  • Correct $consulta = $conexao->query("SELECT * FROM usuarios WHERE loginUsuario='$loginUsuario' AND senhaUsuario = '$senhaUsuario'"); `

  • this la $query = $connected->query("SELECT * FROM users WHERE loginUsuario='$loginUsuario' AND passwordUsuario = '$passwordUsuario'");

  • Was this your ne? I was making the wrong syntax of the query, although the editor did not give as an error in the code, the environment showed an error of not having property in the codigo num_rows, the user @Leo Caracciolo showed the correct form and my error.

0

For question of seized I will be providing the entire code via text on the link

Code

<?php
    ini_set('display_errors', true); error_reporting(E_ALL);

    // inclusao do arquivo de conexao ao banco de dados
    include("conexao.php");

    // se existir o clicar do botao logar do formulario
    if(isset($_POST['logar'])){

        // se existir o login do usuario digitado
        if(isset($_POST['loginUsuario'])){

            // verificação de anti injeção de SQL do campo login do usuario
            $loginUsuario = $conexao->escape_string($_POST['loginUsuario']);

            //se existir a senha do usuario digitado
            if(isset($_POST['senhaUsuario'])){

                // verificação de anti injeção de SQL do campo senha do usuario
                $senhaUsuario = $conexao->escape_string($_POST['senhaUsuario']);

                //************** correção foi aqui ************************* 
                $consulta = $conexao->query("SELECT * FROM usuarios WHERE loginUsuario='$loginUsuario' AND senhaUsuario = '$senhaUsuario'");
                $linha = $consulta->num_rows;

                echo $linha;

            }// isset do campo senha do usuario
        }// isset do campo login do usuario
    }//isset botão logar

    // inclusão do arquivo cabeçario da pagina HTML5
    include("header.php");    
?>
 <center>
    <h1>Entre para logar</h1>
 <!-- formulario de login e senha do usuario -->
 <form action="login.php" method="post">
    <input type="text" name="loginUsuario" maxlength="15" size="15" placeholder="Usuario">
    <input type="password" name="senhaUsuario" maxlength="15" size="15" placeholder="********">
    <input type="submit" name="logar" value="Logar">    
 </form>
</center>

<?php
    // inclusão do arquivo rodape do HTML5
    include("footer.php");
    //fechar banco de dados 
    $conexao->close();
 ?>
  • corrected column names here too :)

  • Ball show, that was my problem, thank you. Question answered.

  • Now I can go back to school, Thank you all.

  • do not forget to mark an answer as accepted, see how and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

  • opens a reply from you, that I can not mark pq is my own answer even you editing can not mark

  • The answer was not complete but it was correct, only attacking the critical points, in which way I started putting the complete code

  • So you put an answer with the code that you changed, so I mark it in your name and put some notes of the problem to someone else when I came to see be oriented

Show 2 more comments

0

Who has the return information of query lines is Resource and not the connection.

Change:

$linha = $conexao->num_rows($resultado);

To:

$linha = $resultado->num_rows;
  • I altered and look there another error, I am coming to you for I am 3 days searching on the internet and testing in all ways.

Browser other questions tagged

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