People, I have a database with a table called "users", and I would like to know how I can, through PHP, count how many records there are in this table, and then give an echo to show this amount on the page. Please help me !!
People, I have a database with a table called "users", and I would like to know how I can, through PHP, count how many records there are in this table, and then give an echo to show this amount on the page. Please help me !!
Just do it that way here.
$sql = "SELECT * FROM usuarios";
$resultado = count($sql);
echo $resultado;
When you make a query in the database, you can pass Count:
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
$query = mysql_query("SELECT count(*) as total from tabela");
$resultado = mysql_fetch_assoc($query);
echo $resultado['total'];
PHP has a function only to count the rows without using Count(*)
which is mysqli_num_rows()
. This allows you to reuse the query result.
$QUERY = "SELECT * AS Quantidade FROM BancoA.tabelaA";
$executa_query = mysqli_query($conexao, $QUERY);
$conta_linhas = mysqli_num_rows($executa_query);
echo $conta_linhas;