Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:33:00+00:00 2026-06-02T16:33:00+00:00

There are two tables, which have a relationship(one-to-many). There are Meals table which contains

  • 0

diagram

There are two tables, which have a relationship(one-to-many). There are Meals table which contains values such as Breakfast(id: 1), Brunch(id: 2), Lunch(id: 3), Tea(id: 4), Dinner(id: 5) and Supper(id: 6). Then there are Foods table which contains various foods and each food(i.e. Hamburger) is linked to a particular type of meal(i.e. Lunch) via a foreign key. So, the row idfood: 12; idmeal: 3; foodname: "Hamburger" would mean that "Hamburger" is of Lunch meal type.

I created an XML generator file which pulls the contents of the whole Food table into an XML file. Problem is I want to translate idmeal foreign key values into actual meal names from the Meal table for each food element inside XML.

The simple way would be to include the foreign key number from Food table, but that would be too cryptic(i.e. Hamburger is eaten during 3 time). Instead I wanted to make it so based on whatever the foreign key number is – the XML would insert the right value from the Meals tables(mealname) into that one XML file with foods so that I could print something like “Hamburger is eaten during Lunch time”.

GenerateXML.php:

<?php
require("includes/credentials.php");

$doc = new DOMDocument("1.0");
$node = $doc->createElement("foods");
$parnode = $doc->appendChild($node);

$connection=mysql_connect($host, $usr, $pass);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

$query = "SELECT * FROM food";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

while ($row = @mysql_fetch_assoc($result)){

  $node = $doc->createElement("food");
  $newnode = $parnode->appendChild($node);

  $newnode->setAttribute("idfood", $row['idfood']);
  $newnode->setAttribute("idmeal", $row['idmeal']);
  $newnode->setAttribute("foodname", $row['foodname']);
}

echo $xmlfile = $doc->saveXML();

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:33:00+00:00Added an answer on June 2, 2026 at 4:33 pm

    join the tables in your query to make use of your FK relationship.

    Select meals.mealname, food.foodname from food join meals on (food.mealid = meals.mealid)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with a many to one relationship which represent lots and
So I have two tables, article and comments (which has one-to-many relationship (1 article
I have a two tables which are linked by a one-to-many relationship. Now I
I have two tables that should be in a one-to-many relationship but there seems
We have two tables, ActivityForm and Field which are given a many-to-many relationship via
Let's say I have two tables with a one-to-many relationship. I want to create
I have two tables books and tags which are in a many-to-many relationship via
I have two tables that have a one-to-many relationship table1 (one), table2 (many). table1
I have two tables which have some transactional stuff stored in them. There will
I have a table in which there are two columns : 1. import type,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.