Using mysqli to count records

Asked

Viewed 5,794 times

4

I need to do a log count. I wore:

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"

  • Place the previous snippet to the line of mysqli_num_rows()... error says your query(return from mysqli_query()) failed.

  • according to the error the variable $query is empty, use echo $consulta; or var_dump($consulta); to see if there’s anything...

  • Tai @rray, the excerpt before mysqli_num_rows, reissued the post and put.

  • @Rafaelacioly, the variable is not empty... Gave an echo on it, I ran the SQL code of the database and shows data.

  • echo $tabela_usuarios shows what ?

  • 1

    Sincerely @Gustavosevero I suggest you learn PDO because soon mysqli will not "exist", it will be depreciated...

  • @Diegosouza, show this: string(88) "SELECT * FROM Owner WHERE owner_email='[email protected]' AND password='xxx180398' "

  • Why don’t you use SELECT COUNT(owner_email) AS TOTAL FROM owner WHERE owner_email='$email' AND password='$password' , ai you give a fetch on the result and do validation taking the data from the total field.

  • @Henriquedomingospereira, do I have to fetch a result? I don’t know mysqli.

  • I put as an answer, take a look.

  • @Rafaelacioly, where do I download the PDO class? Or, how do I install it?

  • @RFL, so far there is no evidence that mysqli will be depreciated. Now if vc refers to a future possibility, in fact in the future any native function of PHP can become deprecated, it is at the discretion of the developer.

Show 7 more comments

1 answer

3

One way to use is to count directly into 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 safer 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 = http://php.net/manual/en/class.mysqli-stmt.php

  • 1

    Beauty @Henriquedomingospereira, got... Only then I use this to get the values of the query: $owner_id = mysqli_use_result($resul, 0, "owner_id"); $username = mysqli_use_result($resul, 0, "owner_firstName"); E the following messages appear on the screen: Warning: mysqli_use_result() expects Exactly 1 Parameter, 3 Given in /home/petsm976/public_html/php/logon.php on line 21 Warning: mysqli_use_result() expects Exactly 1 Parameter, 3 Given in /home/petsm976/public_html/php/logon.php on line 22

Browser other questions tagged

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