Listing and Counting Rows from a table in Mysql

Asked

Viewed 483 times

-2

I am trying to make a list of users who have logged in and at the same time a count of how many times each user has logged in, from log logins in a table, I can not explain very well, so will an example:

Mysql table (log logins):

Nome | Hora_de_Login
----------------------
Joao | 1519935575
Joao | 1519935475
Joao | 1519935275
Pedro| 1519935775
Pedro| 1519932575

And I would like PHP to print like this:

Nome | Numero_de_Logins
----------------------
Joao | logou 3 vezes
Pedro| logou 2 vezes
  • Gives a count with group by in your consultation I believe you will solve.

1 answer

1


SQL:

SELECT Nome, count(Nome) 
FROM A sua tabela
GROUP by Nome

In PHP:

$sql = "SELECT Nome, count(Nome) FROM AsuaTabela GROUP BY Nome";
$result = $asuaVariavelDeConexão->query($sql);
while($usuarios = mysqli_fetch_array($result)){
   echo $usuarios['Nome'] .'logou'. $usuarios['count(Nome)'] .'vezes';
}

There are several ways to do what you want, this is just one of them.

Browser other questions tagged

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