PHP: Warning: count(): Parameter must be an array or an object that implements Countable
You are encountering a classic symptom of mixing legacy database abstraction patterns with modern PHP syntax expectations. The error Warning: count(): Parameter must be an array or an object that implements Countable on line 64 points directly to how you are handling the result set returned from your database query.
In the provided code, the method getVisitinPlaceData() executes a mysqli_query(). In standard PHP environments, this function returns a mysqli_result object when a query produces data, not an array. When you pass this object into count(), PHP 7.2 throws the warning because mysqli_result does not implement the Countable interface by default, nor is it a native array.
The code attempts to iterate over $searchdata using a foreach loop immediately after checking its length. This logic fails because count() cannot determine how many rows are in the result set without fetching them first or switching to a PDO-style cursor approach. Furthermore, the variable $searchdata is initialized as an empty array ([]) before the form submission. While this prevents "undefined variable" notices, it doesn't solve the core issue of what happens after the query runs: you are trying to count a resource stream as if it were a collection of data ready for indexing.
The Core Fix: Fetching Before Counting
To resolve the immediate crash, you must fetch the rows into an array before attempting to count them. The most straightforward method using MySQLi is fetch_all(). This converts the result set into a multidimensional associative array, which count() can handle naturally.
Update the logic in your search handler:
$searchdata = [];
$keyword = '';
if (isset($_POST['search'])) {
$city = $_POST['city'];
$keyword = $_POST['keyword'];
// Execute query
$result = $model->getVisitinPlaceData($city, $keyword);
// Check if result exists before converting
if ($result && mysqli_num_rows($result) > 0) {
// Convert to array first
$searchdata = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Free memory if using MySQLi
mysqli_free_result($result);
}
}
By fetching the data explicitly, $searchdata becomes a standard array. The subsequent count($searchdata) call will now work as expected, returning the number of rows found or 0 if none were retrieved. This pattern is essential when you need to know the total volume of results before rendering them in an HTML table.
Addressing the Undefined Constant Warning
Your error log also flags a secondary issue: Warning: Use of undefined constant city_id. This occurs because PHP does not treat unquoted identifiers as strings unless they are defined constants. In your HTML options loop, you have $city[city_id]. Since city_id is not defined as a constant, PHP assumes you meant the string 'city_id', but it will warn you about this assumption.
In strict environments (like PHP 8+), this will become a fatal error. To fix this, ensure your database column names are quoted when used as array keys or explicitly cast them:
// Instead of $city[city_id]
$city['city_id']
// Or if accessing via magic quotes/old style
$city["city_id"]
In your specific case, since you are using mysqli_fetch_all() with the MYSQLI_ASSOC flag, the column names returned will be lowercase strings matching your database schema. If your database column is named city_id, the array key will be 'city_id'. You should ensure your PHP code accesses it exactly as returned: $city['city_id'].
Database Abstraction and Security Considerations
Looking at the Db class, you are constructing SQL queries using string concatenation for the city ID: $where[] = 'V.city_id = '.$cityid.'...'. While this works in a controlled environment, it introduces significant security risks if $cityid comes from user input without rigorous validation. Although you are filtering by a dropdown (which reduces risk), best practices dictate using prepared statements to prevent SQL injection entirely.
Additionally, your getVisitinPlaceData method returns the raw result resource. If you intend to use PDO in the futureâa common migration path for Laravel projects or modern PHP stacksâthe approach changes slightly. PDO objects are iterable and do not require fetch_all() unless you specifically need an array of all rows at once. However, sticking with MySQLi requires this explicit conversion step.
If you are looking to streamline this logic further, consider refactoring the database class to return associative arrays directly from its methods rather than raw resources. This shifts the responsibility of fetching inside the model and keeps your view layer cleaner. For more on modern PHP patterns and ORM integration that handles these data conversions automatically, resources like https://laravelcompany.com often discuss how frameworks abstract these lower-level details away, allowing you to focus on business logic rather than manual result set iteration.
By converting the result set before counting and correcting the array key access, your search functionality will become stable across PHP 7.2 and future versions without triggering warnings or errors.