Using mysqli to count records

3

I need to do a log count. I used:

mysql_num_rows($consulta);

How do I do the same thing using mysqli? Because I'm doing it this way:

$tabela_usuarios = "SELECT * FROM owner WHERE owner_email='$email' AND password='$password' ";

$resul = mysqli_query($con, $tabela_usuarios);
$regs = mysqli_num_rows($resul);

And the following message appears on the screen: "Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, null given in /home/petsm976/public_html/php/logon.php on line 15"

    
asked by anonymous 21.08.2015 / 17:23

1 answer

3

One way to use it is to do the direct count in SQL, like this:

<?php
$query = "SELECT COUNT(owner_email) AS TOTAL FROM owner WHERE owner_email='$email' AND password='$password'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

echo $row['TOTAL']; 
?>

And in a more secure way, using statement:

$connection = mysqli_connect("host","usuario","senha","banco de dados"); 
$query = "SELECT COUNT(owner_email) AS TOTAL FROM owner WHERE owner_email=? AND password=?";

    if ($stmt = mysqli_prepare($connection, $query))
    {
        mysqli_stmt_bind_param($stmt, $email, $password);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $total);
        mysqli_stmt_fetch($stmt);   
        mysqli_stmt_close($stmt);
    }

    echo $total;

Reference = link

    
21.08.2015 / 19:29