PHP – MySQL min(),max() aggregate operations
In this article, we are going to find minimum and maximum details from the table column using PHP from MySQL Xampp server. We are taking an example from employee database to find the minimum and maximum salary of the employee.
Requirements –Xampp server
- PHP –
It stands for Hyper Text Preprocessor which is used to create dynamic web pages. We are going to connect PHP to my SQL xampp server.
- xampp server –
It is a cross-platform which is used to store the databases locally.
- MySQL –
It is a query language which is used to manage the databases.
Min(), max() aggregate operations :
This is an aggregate function which is used to return the minimum value in the given sql expression. This can be used along with SELECT statement.
select min(column_name) as minimum_column_name from table_name;
Here, as minimum_column_name is an optional one.
This is an aggregate function which is used to return the maximum value in the given SQL expression. This can be used along with SELECT statement.
select max(column_name) as maximum_column_name from table_name;
Here, as maximum_column_name is an optional one.
- Create database (database) and table(salary) in xampp server.
- Write a code to insert details in a salary table using PHP.
- PHP’s code to find the minimum and maximum salary using min() and max() functions.
- Observe the output on a web page.
Steps to start the server and storing data :
- Start the xampp server.
- Create a database named database and table salary in xampp. Type “localhost/phpmyadmin” in your browser.
PHP’s code to insert records (data1.php) :
Records inserted in xampp server as follows.
emp_name salary department sravan 35000 IT sudheer 45000 IT radha 25000 MCA vani 35000 BCA
PHP code to find minimum salary(form.php) :
PHP code to find maximum salary :
PHP code to display both minimum and maximum salary :