How can one efficiently handle multiple table queries in PHP to find a specific entry?

When handling multiple table queries in PHP to find a specific entry, it is important to use SQL JOIN statements to connect the tables based on their relationships. By using JOINs, you can retrieve data from multiple tables in a single query, which is more efficient than making separate queries for each table. Additionally, using WHERE clauses can help filter the results to find the specific entry you are looking for.

<?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);
}

// Query to find a specific entry using JOIN and WHERE
$sql = "SELECT * FROM table1
        JOIN table2 ON table1.id = table2.table1_id
        WHERE table1.id = 1";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>