What are the best practices for joining tables in MySQL queries within PHP applications?

When joining tables in MySQL queries within PHP applications, it is important to use proper aliases for table names, specify the columns to be selected explicitly, and use prepared statements to prevent SQL injection attacks.

<?php
// Establish a database connection
$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);
}

// Prepare and execute a query with table joins
$sql = "SELECT users.username, orders.order_id FROM users
        JOIN orders ON users.user_id = orders.user_id
        WHERE users.status = 'active'";

$stmt = $conn->prepare($sql);
$stmt->execute();

// Fetch the results
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    echo "Username: " . $row['username'] . " - Order ID: " . $row['order_id'] . "<br>";
}

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