Count the Number of Records with Conditionals in PHP 7.3

I am slowly converting some PHP 5.6.x scripts over to PHP 7.3 and I’ve been running into a wall with getting the code below to work. If I run the query in phpMyAdmin, it returns 88 records - which is the correct number. However, when I run the code below, it either returns a number of 45 records, or “Array”, or blank.

Can anyone point out what I’m doing wrong with suggestions?

// 1. Create a database connection
$connection = mysqli_connect(“localhost”,“USERNAME”,“PASSWORD”);
if (!$connection) {
die(“Database connection failed”);

// 2. Select a database to use
$db_select = mysqli_select_db($connection, “DATABASE-NAME”);
if (!$db_select) {
die("Database selection failed: " . mysqli_error($connection));

$ct_2004_destroyed = mysqli_query($connection, “SELECT COUNT(*) as count_2004_destroyed FROM registry_xlr WHERE model_year = ‘2004’ AND status = ‘Destroyed’”);
$count_2004_destroyed = mysqli_fetch_array($ct_2004_destroyed);


2004s Destroyed:  <? print "$count_2004_destroyed";?>

mysqli_query can fail; you should check that $ct_2004_destroyed is not false the same way you do with $db_select.

Once you know $ct_2004_destroyed is a valid result, mysqli_fetch_array($ct_2004_destroyed) will fetch an array representing the first row of the query result. It doesn’t matter that the query you’re running only has one result, or it only has one value in it; this is how the db functions work.

So $count_2004_destroyed will have be array, probably something like:

array(0 => "122", 'COUNT(*)' => "122")

So you can print your result with:

<?php print $count_2004_destroyed['COUNT(*)'] ?>