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
Introduction :
- 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 :
min() –
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.
Syntax :
select min(column_name) as
minimum_column_name from table_name;
Here, as minimum_column_name is an optional one.
max() –
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.
Syntax :
select max(column_name) as maximum_column_name from table_name;
Here, as maximum_column_name is an optional one.
Approach :
- 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 :
- Create a database named database and table salary in xampp. Type “localhost/phpmyadmin” in your browser.
PHP’s code to insert records (data1.php) :
PHP
<?php
$servername = "localhost" ;
$username = "root" ;
$password = "" ;
$dbname = "database" ;
$conn = new mysqli( $servername , $username , $password , $dbname );
if ( $conn ->connect_error) {
die ( "Connection failed: " . $conn ->connect_error);
}
$sql = "INSERT INTO salary VALUES ('sravan',35000,'IT');" ;
$sql .= "INSERT INTO salary VALUES ('sudheer',45000,'IT');" ;
$sql .= "INSERT INTO salary VALUES ('radha',25000,'MCA');" ;
$sql .= "INSERT INTO salary VALUES ('vani',35000,'BCA');" ;
if ( $conn ->multi_query( $sql ) === TRUE) {
echo "data stored successfully" ;
} else {
echo "Error: " . $sql . "<br>" . $conn ->error;
}
$conn ->close();
?>
|
Output :
localhost/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
<html>
<body>
<?php
$servername = "localhost" ;
$username = "root" ;
$password = "" ;
$dbname = "database" ;
$conn = new mysqli( $servername , $username , $password , $dbname );
$sql = "SELECT MIN(salary) FROM salary" ;
$result = $conn ->query( $sql );
while ( $row = mysqli_fetch_array( $result )){
echo "Minimum Salary :" . $row [ 'MIN(salary)' ];
echo "<br />" ;
}
$conn ->close();
?>
</body>
</html>
|
Output :
localhost/form.php
PHP code to find maximum salary :
(form.php)
PHP
<html>
<body>
<?php
$servername = "localhost" ;
$username = "root" ;
$password = "" ;
$dbname = "database" ;
$conn = new mysqli( $servername , $username , $password , $dbname );
$sql = "SELECT MAX(salary) FROM salary" ;
$result = $conn ->query( $sql );
while ( $row = mysqli_fetch_array( $result )){
echo "Maximum Salary :" . $row [ 'MAX(salary)' ];
echo "<br />" ;
}
$conn ->close();
?>
</body>
</html>
|
Output :
localhost/form.php
PHP code to display both minimum and maximum salary :
PHP
<html>
<body>
<?php
$servername = "localhost" ;
$username = "root" ;
$password = "" ;
$dbname = "database" ;
$conn = new mysqli( $servername , $username , $password , $dbname );
$sql = "SELECT MIN(salary),MAX(salary) FROM salary" ;
$result = $conn ->query( $sql );
while ( $row = mysqli_fetch_array( $result )){
echo "Minimum Salary :" . $row [ 'MIN(salary)' ];
echo "<br />" ;
echo "Maximum Salary :" . $row [ 'MAX(salary)' ];
echo "<br />" ;
}
$conn ->close();
?>
</body>
</html>
|
Output :
Last Updated :
30 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...