When should LEFT JOIN be used instead of INNER JOIN in PHP MySQL queries?

LEFT JOIN should be used instead of INNER JOIN when you want to retrieve all records from the left table (the table mentioned first in the query) regardless of whether there is a matching record in the right table. This is useful when you want to include all records from the left table even if there are no corresponding records in the right table.

$query = "SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.table1_id";
$result = mysqli_query($connection, $query);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process the retrieved data
    }
}