I'm trying to display the number of rows in a SQL table with a condition.
The table viaturas has 4 records. I want to count how many rows in the column estado match "Inoperacional", in this case, 3 records.
I've created a function ViaturasInop() in viaturas.php:
<?php
function ViaturasInop()
{
require_once "config.php";
$query = "SELECT * FROM `viaturas` WHERE `estado` LIKE 'Inoperacional'";
if ($result=mysqli_query($link,$query))
$rowcount=mysqli_num_rows($result);
return $rowcount;
mysqli_close($link);
}
echo ViaturasInop();
?>
If run viaturas.php it correctly returns 3.
In the page I want the result to be shown index.php in the HTML body I have the following to call the function:
<div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo ViaturasInop(); ?></div>
However in return I get the following errors:
Notice: Undefined variable: link in C:[...]\php\viaturas.php on line 8
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:[...]\php\viaturas.php on line 8
Notice: Undefined variable: rowcount in C:[...]\php\viaturas.php on line 11
I've tried other alternatives, however, I always get the same error.