-1

I have a table with 5 rows and I want to print the count of all the rows into a html div. I tried it like this:

$sqlassets = "SELECT COUNT(*) FROM assets";
$result=mysqli_query($conn, $sqlassets);
$log=var_dump($result);

and then I tried to use this variable in HTML:

<div class="col mr-2">
<div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Count of cars</div>
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php print($log) ?></div>
</div>

but the result is:

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

$result is an object of class mysqli_result. You need to fetch the first column from the first row. You can do it via fetch_row()[0]

$sqlassets = "SELECT COUNT(*) FROM assets";
$result = $conn->query($sqlassets);
$numOfRows = $result->fetch_row()[0];
var_dump($numOfRows);

You can then use the contents of that variable in the HTML:

<div class="col mr-2">
    <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Count of cars</div>
    <div class="h5 mb-0 font-weight-bold text-gray-800"><?php print($numOfRows) ?></div>
</div>
Dharman
  • 30,962
  • 25
  • 85
  • 135