What are the best practices for sorting data alphabetically in PHP using SQL queries?

When sorting data alphabetically in PHP using SQL queries, it is important to use the ORDER BY clause in your SQL query. This clause allows you to specify the column you want to sort by, as well as the direction of the sorting (ASC for ascending or DESC for descending). By including the ORDER BY clause in your SQL query, you can ensure that the data returned is sorted alphabetically based on the specified column.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "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 table and sort alphabetically
$sql = "SELECT * FROM table_name ORDER BY column_name ASC";
$result = $conn->query($sql);

// Output data
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Column: " . $row["column_name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>