Jump to content

Grabbing information from multiple tables


Drumlegend

Recommended Posts

Currently I am able to pull the recipe name from the recipe table but I want to be able to grab the ingredients required from the ingredients table. I know it's to do with JOINS but I'm new to JOINS.

 

This is the ingredients table

ingredients.png

 

 

This is the recipeingredients table, this has two primary keys so I am able to assign multiple ingredients to one recipe

 

 

recipeingredients.png

 

This is the recipe table

 

recipes.png


 

 

 

 

This is the search script

<?php
$query = $_GET['query'];
// gets value sent over search form

$min_length = 3;


if(strlen($query) >= $min_length){

$query = htmlspecialchars($query);


$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection

$raw_results = mysql_query("SELECT * FROM recipes
WHERE (`recipename` LIKE '%".$query."%') OR (`ingredients` LIKE '%".$query."%')") or die(mysql_error());





if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

echo "<p>Recipe:".$results['recipename']."</p><p>Ingredients:".$results['ingredients']."<p>Instructions:".$results['instructions']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}

}
else{ // if there is no matching rows do following
echo "No results";
}

}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>

This is the sample data from the ingredients table

ingredientssample.png

 

This is the sample data from the recipeingredients table

 

recipeingredientssample.png

This is sample date from the recipe table

 

recipesample.png

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.