What is the best way to sort results of a query in PHP using ORDER BY?

When querying a database in PHP and you want to sort the results in a specific order, you can use the "ORDER BY" clause in your SQL query. This clause allows you to specify the column by which you want to sort the results, as well as the direction (ASC for ascending or DESC for descending). By including "ORDER BY" in your query, you can ensure that the results are returned in the desired order.

// Connect to database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL query to select data from a table and sort it by a specific column in ascending order
$sql = "SELECT * FROM table_name ORDER BY column_name ASC";

// Execute the query
$result = $conn->query($sql);

// Fetch and display the sorted results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Column value: " . $row["column_name"] . "<br>";
    }
} else {
    echo "0 results";
}

// Close the connection
$conn->close();