SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The ORDER BY
clause in MySQL is used to sort the results returned by a query in ascending or descending order based on one or more columns.
Here's how you can use the ORDER BY
clause in PHP when querying a MySQL database:
$servername = "your_server_name"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
To select all records from a table called users
and order them by the name
column in ascending order:
$sql = "SELECT * FROM users ORDER BY name ASC"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>"; } } else { echo "0 results"; }
To order them in descending order:
$sql = "SELECT * FROM users ORDER BY name DESC"; // ... (execute the query and fetch the results as shown above)
To order by name
in ascending order and, in case of duplicates, by id
in descending order:
$sql = "SELECT * FROM users ORDER BY name ASC, id DESC"; // ... (execute the query and fetch the results as shown above)
$conn->close();
Security Reminder: Avoid directly embedding user-input data into your SQL queries due to the risk of SQL injection. For a more secure approach, use prepared statements or a database abstraction layer that provides parameterized queries.
Sorting MySQL results in PHP:
<?php $conn = new mysqli("localhost", "your_username", "your_password", "your_database"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM your_table ORDER BY column_name"; $result = $conn->query($sql); // Process the sorted result set... $conn->close(); ?>
How to use ORDER BY in MySQL with PHP:
$sql = "SELECT * FROM your_table ORDER BY column_name";
PHP MySQL ORDER BY multiple columns:
$sql = "SELECT * FROM your_table ORDER BY column1, column2";
Sorting MySQL query results using PHP:
$column_to_sort = "column_name"; $sql = "SELECT * FROM your_table ORDER BY $column_to_sort";
ORDER BY DESC in PHP with MySQL:
$sql = "SELECT * FROM your_table ORDER BY column_name DESC";
PHP MySQL ORDER BY ascending and descending:
$sort_order = "ASC"; // or "DESC" $sql = "SELECT * FROM your_table ORDER BY column_name $sort_order";
Sorting MySQL data in PHP script:
$sql = "SELECT * FROM your_table ORDER BY column_name";
Dynamic ORDER BY in PHP and MySQL:
$user_sort_column = $_GET['sort_column'] ?? 'default_column'; $sql = "SELECT * FROM your_table ORDER BY $user_sort_column";
Sorting PHP array from MySQL query result:
$sql = "SELECT * FROM your_table"; $result = $conn->query($sql); $rows = []; while ($row = $result->fetch_assoc()) { $rows[] = $row; } usort($rows, function($a, $b) { return $a['column_name'] - $b['column_name']; });
Sorting MySQL query results by column in PHP:
$sort_column = "column_name"; $sql = "SELECT * FROM your_table ORDER BY $sort_column";
PHP MySQL ORDER BY date:
$sql = "SELECT * FROM your_table ORDER BY date_column";
Pagination with ORDER BY in PHP and MySQL:
$page = $_GET['page'] ?? 1; $limit = 10; $offset = ($page - 1) * $limit; $sql = "SELECT * FROM your_table ORDER BY column_name LIMIT $limit OFFSET $offset";
Using ORDER BY in prepared statements with PHP and MySQL:
$sort_column = $_GET['sort_column'] ?? 'default_column'; $stmt = $conn->prepare("SELECT * FROM your_table ORDER BY ?"); $stmt->bind_param("s", $sort_column); $stmt->execute();
MySQL ORDER BY with user input in PHP:
$user_sort_column = $_GET['sort_column'] ?? 'default_column'; $sql = "SELECT * FROM your_table ORDER BY $user_sort_column";