What are the recommended approaches for handling SQL queries in PHP to properly link recipe data with ingredient data in a relational database structure?

When handling SQL queries in PHP to link recipe data with ingredient data in a relational database structure, it is recommended to use JOIN clauses to combine data from multiple tables based on a related column. This allows for retrieving all necessary information in a single query rather than making multiple queries. Additionally, using prepared statements with placeholders can help prevent SQL injection attacks.

// Example SQL query to retrieve recipe data along with its ingredients using a JOIN clause
$query = "SELECT recipes.recipe_name, ingredients.ingredient_name
          FROM recipes
          JOIN recipe_ingredients ON recipes.recipe_id = recipe_ingredients.recipe_id
          JOIN ingredients ON recipe_ingredients.ingredient_id = ingredients.ingredient_id
          WHERE recipes.recipe_id = :recipe_id";

// Prepare the query
$stmt = $pdo->prepare($query);

// Bind the recipe_id parameter
$stmt->bindParam(':recipe_id', $recipe_id, PDO::PARAM_INT);

// Execute the query
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Loop through the results and display the recipe name and ingredient name
foreach ($results as $row) {
    echo $row['recipe_name'] . " - " . $row['ingredient_name'] . "<br>";
}