What is the best way to check if the main content of an object fetched via fetchAll() is empty in PHP?
When using fetchAll() in PHP to retrieve data from a database, the result is returned as an array of rows. To check if the main content of this object is empty, you can simply use the empty() function to check if the array is empty or not. This will allow you to determine if there are any rows fetched from the database or not.
// Fetch data from database
$data = $stmt->fetchAll();
// Check if main content is empty
if (empty($data)) {
echo "No data found.";
} else {
// Process fetched data
foreach ($data as $row) {
// Do something with each row
}
}
Keywords
Related Questions
- How can the issue of headers already being sent be prevented in PHP?
- How can media queries in CSS be effectively used to create responsive design without the need for PHP screen width detection?
- How can the issue of not passing the ID from one PHP file to another be resolved in a form submission process?