PHP MySQL - ORDER BY clause
Let’s discuss how to apply the ORDER BYclause with the SQL SELECT Query through PHP to connect with the MySQL database in the XAMPP Server.
Consider the table - Medicine present in the database - hospital snapshot with some records.
.
In sql, ORDER BY is used to sort the result set.
Sorting by default is in ascending order. It means if nothing is mentioned then result set will be sorted in ascending order.
Alternatively, you can also instruct to sort in ascending order by keyword ASC on ORDER BY.
To sort in descending order use keyword DESC.
Syntax for ORDER BY ASC
SELECT [*|columns]
FROM table_name
ORDER BY column1, column2,..., columnN ASC/DESC;
Now, let' see how to perform SELECT with ORDER BY clause in xampp server through PHP.
.
PHP MySQL - Object oriented Connection
We will see how to connect PHP with a MySQL database in XAMPP Server using PHP Script using Object oriented Format.
Approach
1.Specify the credentials to create a connection object.
The credentials include XAMPP Server localhost name, username, password and database name.
$connecting_data = new mysqli($server, $user, $pwd, $data);
2.Specify the SQL Query that includes the ORDER BY clause.
$my_query = SELECT columns… from TABLE_NAME ORDER BY column1, column2,..., columnN ASC/DESC;
3.Get the query into the connection object.
$return = $connecting_data->query($my_query);
4.Return the records from the result query.
if ($return->num_rows > 0) {
while($record_data = $return->fetch_assoc()) {
// Display the data
}
} else {
echo "No Data";
}
5.Close the connection object
$connecting_data->close();
Example 1-
Return the records from the Medicine Table in Ascending order by Quantity column.
<?php
// Specify the server
$server = "localhost";
// Specify the user
$user = "root";
// Specify the password
$pwd = "";
// Specify the database
$data = "hospital";
// Let's create connection by using the above details
$connecting_data = new mysqli($server, $user, $pwd, $data);
// Mention the SQL Query to get rows from Medicine in Ascending order by Quantity.
$my_query = "SELECT * FROM Medicine ORDER BY Quantity ASC";
$return = $connecting_data->query($my_query);
if ($return->num_rows > 0) {
while($record_data = $return->fetch_assoc()) {
echo "Disease: " . $record_data['Disease']. ", Tablet: " . $record_data['Tablet']. ", Quantity: " . $record_data['Quantity']." , Cost: " . $record_data['Cost']."<br>";
}
} else {
echo "No Data";
}
$connecting_data->close();
?>
Output:

We can see that rows are returned in ascending based on the values in Quantity column.
Example 2-
Return the records from the Medicine Table in Descending order by Quantity column.
<?php
// Specify the server
$server = "localhost";
// Specify the user
$user = "root";
// Specify the password
$pwd = "";
// Specify the database
$data = "hospital";
// Let's create connection by using the above details
$connecting_data = new mysqli($server, $user, $pwd, $data);
// Mention the SQL Query to get rows from Medicine in Descending order by Quantity.
$my_query = "SELECT * FROM Medicine ORDER BY Quantity DESC";
$return = $connecting_data->query($my_query);
if ($return->num_rows > 0) {
while($record_data = $return->fetch_assoc()) {
echo "Disease: " . $record_data['Disease']. ", Tablet: " . $record_data['Tablet']. ", Quantity: " . $record_data['Quantity']." , Cost: " . $record_data['Cost']."<br>";
}
} else {
echo "No Data";
}
$connecting_data->close();
?>
Output:

We can see that rows are returned in descending based on the values in Quantity column.
Example 3:
It can also be possible to specify ORDER BY with WHERE Clause.
Now we will get the rows in Descending order based on Quantity Column with vallues greater than 30.
<?php
// Specify the server
$server = "localhost";
// Specify the user
$user = "root";
// Specify the password
$pwd = "";
// Specify the database
$data = "hospital";
// Let's create connection by using the above details
$connecting_data = new mysqli($server, $user, $pwd, $data);
// Mention the SQL Query to get rows from Medicine with Quantity > 30 in Descending order by Quantity.
$my_query = "SELECT * FROM Medicine WHERE Quantity > 30 ORDER BY Quantity DESC";
$return = $connecting_data->query($my_query);
if ($return->num_rows > 0) {
while($record_data = $return->fetch_assoc()) {
echo "Disease: " . $record_data['Disease']. ", Tablet: " . $record_data['Tablet']. ", Quantity: " . $record_data['Quantity']." , Cost: " . $record_data['Cost']."<br>";
}
} else {
echo "No Data";
}
$connecting_data->close();
?>
Output:

We can see that rows with greater than 30 from Quantity Column is returned in Descending order.
Conclusion
So we seen how to order the rows in ascending / descending order using ORDER BY through PHP in XAMPP Server. It can be possible to use ORDER BY with WHERE Clause.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 14,2024