How can a single query be structured to retrieve both the count and additional data from a database table in PHP?

When retrieving both the count and additional data from a database table in PHP, you can use the SQL COUNT() function along with a regular SELECT query. This allows you to get the total count of rows that match your query criteria, as well as fetch the actual data rows themselves in a single query. By using this approach, you can reduce the number of queries needed to fetch the required information from the database.

<?php

// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');

// Define your query to retrieve both count and additional data
$query = "SELECT COUNT(*) as total_count, column1, column2 FROM your_table WHERE your_condition";

// Execute the query
$result = $connection->query($query);

// Fetch the count and additional data
$row = $result->fetch_assoc();

// Output the total count
echo "Total count: " . $row['total_count'] . "<br>";

// Output additional data
echo "Additional data:<br>";
echo "Column 1: " . $row['column1'] . "<br>";
echo "Column 2: " . $row['column2'] . "<br>";

// Close the database connection
$connection->close();

?>