How can one specify which column to access when multiple tables have columns with the same name in a SQL query result in PHP?

When multiple tables have columns with the same name in a SQL query result, you can specify which column to access by aliasing the column names in the query. By assigning unique aliases to the columns in the SELECT statement, you can differentiate between them in the PHP code when fetching the results.

$query = "SELECT table1.column_name AS column1, table2.column_name AS column2 FROM table1, table2 WHERE table1.id = table2.id";
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result)) {
    $value1 = $row['column1'];
    $value2 = $row['column2'];
    
    // Access the values of the specified columns using their aliases
}