Joins Query using php




The task of joining MySQL mentions destroying two or more tables in a table. This means that whatever you have learned so far can be applied even after applying everything, to join the table.

family Table:

PositionAge
Dad41
Mom45
Daughter17
Dog

food Table:

MealPosition
SteakDad
SaladMom
Spinach Soup
TacosDad

PHP and MySQL Code:

<?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT family.Position, food.Meal ".
 "FROM family, food ".
 "WHERE family.Position = food.Position";
  
$result = mysql_query($query) or die(mysql_error());


// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
 echo $row['Position']. " - ". $row['Meal'];
 echo "<br />";
}
?>
The statement "WHERE family.Position = food.Position" will restrict the results to the rows where the Position exists in both the "family" and "food" tables.

Display:

Dad - Steak
Mom - Salad
Dad - Tacos
Those are the results of our PHP script. Let's analyze the tables to make sure we agree with these results.

Compare the Tables:

PositionAge
Dad41
Mom45
Daughter17
Dog
MealPosition
SteakDad
SaladMom
Spinach Soup
TacosDad

Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment