开发者

how to retrieve xml data from three tables of mysql

I want to retrieve xml data from three table stored in MySQL. I used the below code it works fine but first it retrieve one record from first table than iterate to second table and print the whole table and then iterate to third table and print the whole table but I want to print first table along with relevant records in second table (not whole table) then from third table and so on. My code is:

$table_first = 'recipe';
$query = "SELECT * FROM $table_first";
$resouter = mysql_query($query, $conn);

$table_second='instructions';
$query="SELECT instructions.instruction_id,instructions.instruction_text FROM $table_second";
$resinner=mysql_query($query, $conn);


$table_third='ingredients';

$query="SELECT ingredients.ingredient_id,ingredients.ingredient_name,ingredients.ammount FROM $table_third";
$resthird=mysql_query($query, $conn);


$doc = new DomDocument('1.0');

$root = $doc->createElement('recipes');
$root = $doc->appendChild($root);




while($row = mysql_fetch_assoc($resouter)){


$outer = $doc->createElement($table_first);
$outer = $root->appendChild($outer);

 foreach ($row as $fieldname => $fieldvalue) {
    $child = $doc->createElement($fieldname);
    $child = $outer->appendChild($child);
    $value = $doc->createTextNode($fieldvalue);
    $value = $child->appendChild($value);
  }// foreach
 //while
$inner = $doc->createElement($table_second);
    $inner = $outer->appendChild($inner);
 while($row = mysql_fetch_assoc($resinner)){
    // add node for each record
    

    $inner1=$doc->createElement('instruction');
    $inner1=$inner->appendChild($inner1);
    // add a child node for each field
    foreach ($row as $fieldname => $fieldvalue) {
        $child = $doc->createElement($fieldname);
        $child = $inner1->appendChild($child);
        $value = $doc->createTextNode($fieldvalue);
        $value = $child->appendChild($value);
    } // foreach
 }// while


 $inner=$doc->createElement($table_third);
    $inner=$outer->appendChild($inner);
    
while($row=mysql_fetch_assoc($resthird)){



     $inner2=$doc->createElement('ingredient');
    $inner2=$inner->appendChild($inner2);

    foreach($row as $fieldname=> $fieldvalue)
    {
        $child=$doc->create开发者_StackOverflow中文版Element($fieldname);
        $child=$inner2->appendChild($child);
        $value=$doc->createTextNode($fieldvalue);
        $value=$child->appendChild($value);
    }
}
}

mysql_close($conn);
$xml_string = $doc->saveXML();
echo $xml_string;


There is a much better way to accomplish what you're doing. It looks as though you pulling data from 3 different tables, using three different SQL queries, and three calls to the database.

The best way to accomplish this would be to build your table on the SQL side, using a single query. The queries are a little more complicated (you'll need JOIN) however the result will be much easier to work with in your program. You'll also probably avoid having to create your document on the fly using DOM. You could simple build your recipe using SQL, pull the whole result back and simply iterate through the table with the result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜