What are potential pitfalls when using aliases in MySQL queries in PHP?

Potential pitfalls when using aliases in MySQL queries in PHP include using the alias in the SELECT statement instead of the original column name, which can lead to errors. To avoid this, always use the alias in the ORDER BY or GROUP BY clauses. Additionally, be cautious when using aliases in WHERE clauses as some databases may not support this.

$query = "SELECT column_name AS alias_name FROM table_name ORDER BY alias_name";
$result = mysqli_query($connection, $query);

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Access the aliased column value using the alias_name
        echo $row['alias_name'];
    }
} else {
    echo "Error: " . mysqli_error($connection);
}