Show on page total id’s registered in BD

Asked

Viewed 87 times

0

There is a table that shows all BD records, but on a page of my project where I should show the record total(id) of my table.

Connection file:

<?php
$mysqli = new mysqli("localhost", "house863_teste", "teste", "house863_registro");
if ($mysqli->connect_errno) {
echo "Falha ao conectar com o mysql: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}?>

File that brings data to HTML table:

<?php
require_once("config.php");

$query = "SELECT * FROM lista ";

$result = mysqli_query($mysqli, $query);

$x=1;   
while($row = mysqli_fetch_assoc($result))
{
echo("
<tr>
    <td class=\"n\">".$row['id']."</td>
    <td class=\"nm\">".$row['nome']."</td>
    <td><span class=\"cont\" id=\"s".$x."\" inicial=\"".$row['tempo']."\"></span></td>
    <td class=\"qp\">".$row['pm']."</td>
    <td class=\"sm\">".$row['dific']."</td>
    <td class=\"op\">".$row['op']."</td>
    <td class=\"btnt\"><button class=\"btn1\" onclick=\"dispara('s".$x."')\"><a target=\"_blank\" href=\"".$row['link']."\">Clique aqui</a></button></td>
</tr>");
$x=$x+1;   
}

echo("</tbody>");
?>

After making the connection, as I can show on the page the total of id's registered?

Html example:

Total de registros(<?php require_once("total.php");?>)

2 answers

2


You could bring in your own consultation

<?php 
require_once("config.php");
$query = "SELECT count(*) FROM lista ";
$total = mysqli_query($mysqli, $query);

The Count function already shows the number of records in the table.

-2

<?php
require_once("config.php");

$query = "SELECT * FROM lista ";

$result = mysqli_query($mysqli, $query);

$total = mysqli_num_rows($result); 

The above code takes the number of query lines $result and plays within the variable total.

echo $total;
$x=1;   
while($row = mysqli_fetch_assoc($result))
{
echo("
<tr>
    <td class=\"n\">".$row['id']."</td>
    <td class=\"nm\">".$row['nome']."</td>
    <td><span class=\"cont\" id=\"s".$x."\" inicial=\"".$row['tempo']."\"></span></td>
    <td class=\"qp\">".$row['pm']."</td>
    <td class=\"sm\">".$row['dific']."</td>
    <td class=\"op\">".$row['op']."</td>
    <td class=\"btnt\"><button class=\"btn1\" onclick=\"dispara('s".$x."')\"><a target=\"_blank\" href=\"".$row['link']."\">Clique aqui</a></button></td>
</tr>");
$x=$x+1;   
}

echo("</tbody>");
?>
  • Could explain the code?

  • Of course, mysqli_num_rows($result); will return the number of rows in the "$result" query, this number will be played inside the "$total" variable and the <?php echo $total ? > will write on tele the number.

  • Leone edits and puts in response.

Browser other questions tagged

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