Trying to generate XML file from returend SQL query results.
The SQL query returns ItemId, ItemName, ItemDescription from Items table.
I want to list the ItemNames inside XML like so:
XML
...
<item>
<name>ItemName1</name>
</item>
<item>
<name>ItemName2</name>
</item>
...
PHP
...
while($items = @mysql_fetch_assoc($query)){
foreach($items as $row){
}
$node = $doc->createElement('item');
$node = $root->appendChild($node);
$child = $doc->createElement('name');
$child = $node->appendChild($child);
$value = $doc->createTextNode($row['ItemName']); // !
$value = $child->appendChild($value);
}}
...
My XML structure generates properly but the problem is I get the first letter of each of the item’s ItemDescription, instead ItemName inside every name element like so:
...
<name>D</name>
...
<name>H</name>
...
First two items have their string descriptions start with letters D and H.
What is causing this behaviour?
If I understand correctly, the problem is the
foreach()which is looping through each key/value pair in the$rowarray. I don’t think this is the behavior you desire, because each row in mysql results set is not usually multi-dimensional. Because the index ‘ItemName’ is not found, it’s defaulting to [0] (the first character of the value).Try just the while() loop: