List and Count Rows of a Table in MySQL

-2

I'm trying to make a listing of users who logged in and at the same time a count of how many times each user logged, from logins records in a table, I do not know how to explain very well,

MySQL table (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
    
asked by anonymous 01.03.2018 / 21:24

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.

    
01.03.2018 / 22:01