Return row count of a query

0

I'm trying to validate a login using row count of a query, but I'm not getting it:

My connection looks like this:

include("defines.php");
try {
  $PDO = new PDO('mysql:host=' . HOST . ';dbname=' . DB, USER, PASS );
  $PDO->exec("set names utf8");
} catch(PDOException $e) {
    echo 'Erro de conexão ao Banco de Dados: ' . $e->getMessage();
}

My form looks like this:

<form class="userform" id="frmLogin" method="post" action="verifica_usuario.php">
    <input type="text" size="35" id="usuario" name="usuario"  value="<?php echo $_POST['usuario']; ?>" />

    <input type="password" size="35" class="bradius" id="senha" name="senha" value="<?php echo $_POST['senha']; ?>" />

    <spam  class="button" ><a href="#" class="button"  onClick="frmLogin.submit();" onMouseOver="window.status='';return true" /> Entrar</a>
    </spam>
   </form>

The page that does the validation looks like this:

session_start();  // Inicia a session
include "libraries/conn.php";

@$usuario = $_POST['usuario'];
@$senha = $_POST['senha'];

$senhacrip = md5($senha);
    $sql = mysql_query("SELECT * FROM usuarios WHERE usuario='{$usuario}' AND senha='{$senhacrip}' AND ativado='S'");
    $login_check = mysql_num_rows($sql);

However, when you run the code, the browser returns the message:

  

Warning: mysql_num_rows () expects parameter 1 to be resource, boolean   given in D: \ xampp \ htdocs \ n_archaeus \ verifies_user.php on line 22

    
asked by anonymous 10.08.2018 / 02:54

1 answer

0

If your connection uses PDO continue to work with PDO

$sql = $PDO->query("SELECT COUNT(*) FROM usuarios WHERE usuario='{$usuario}' AND senha='{$senhacrip}' AND ativado='S'");
$login_check = $sql->fetchColumn();
echo $login_check; //retorna 1 caso haja usuario e 0 se não houver
    
10.08.2018 / 04:12