Home >Database >Mysql Tutorial >How to Retrieve Single Aggregate Query Results in PHP?
Retrieving Aggregate Query Results in PHP
When working with MySQL queries in PHP, it can be challenging to retrieve the single output value when using aggregate functions like COUNT(*). Understanding how to handle such queries is crucial.
In PHP, there are several functions available to work with query results. However, to retrieve the exact value from an aggregate query (such as the example provided), you must alias the aggregate using the as keyword.
Here's how to do it:
$result = mysql_query("SELECT count(*) as total from Students"); $data = mysql_fetch_assoc($result); // Displaying COUNT(*) result echo $data['total'];
By aliasing the aggregate as total with the as keyword, we can call it using mysql_fetch_assoc. This method retrieves the resulting row of the query as an associative array. Hence, we can access the count value using the key 'total' as shown in the example. This approach enables you to work with aggregate query results effectively in PHP.
The above is the detailed content of How to Retrieve Single Aggregate Query Results in PHP?. For more information, please follow other related articles on the PHP Chinese website!