Page 1 of 1
PHP Converts mysql database data into XML
Posted: Fri Aug 18, 2006 8:23 pm
by calmchess
Ok here is what i've got....i have a php script that selects data from a mysql database and then converts it into xml ...the script runs in a infinate loop.....problem is when i test the page i get an error that says "Only one top level element is allowed in an XML document. ".........i know the problem is that i'm running a loop......(top level element is being created over and over again ).......but i need it to run in a loop so that when new data is added to the database the php script automatically converts it into xml........what do you guys think i should do to resolve my issue?

Posted: Fri Aug 18, 2006 9:04 pm
by Ambush Commander
Erm... add/detect a top level tag and insert the data as a descendant.
Posted: Fri Aug 18, 2006 11:05 pm
by calmchess
This discussion is now closed i don't know what i did but i was messing with the php code then hit the refresh button for the page and it worked beautifully. I think it was a syntax error maybe &shrugs&
Posted: Fri Aug 18, 2006 11:19 pm
by RobertGonzalez
For future reference, posting code always increases your chances of getting a knowledgable response.
Posted: Fri Aug 18, 2006 11:27 pm
by calmchess
Everah | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Well It wasn't a script problem.........it was a internet explorer problem............i ran the code in firefox and it worked perfectly...........Internet explorer 7 better be better microsoft * shakes fist * here is the code if anybody intrested
Code: Select all
<?php
//this loop works well at a command prompt but a browser won't display it because the parent element "products" is only allowed once.
while(1) {
$link = mysql_connect("localhost","root","KKT1KKT1");
mysql_select_db("brimelow_store");
$query = 'select * from products';
$results = mysql_query($query);
echo"<?xml version=\"1.0\"?>\n";
echo"<products>\n";
while($line = mysql_fetch_assoc($results)) {
echo "<item>" . $line["products"] . "</item>\n";
}
echo"</products>\n";
mysql_close($link);
}
?>
Everah | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Aug 18, 2006 11:30 pm
by RobertGonzalez
Posted: Fri Aug 18, 2006 11:38 pm
by calmchess
while (1) is how i make it loop forever
Posted: Fri Aug 18, 2006 11:40 pm
by RobertGonzalez
And why do a loop that doesn't end?
Posted: Fri Aug 18, 2006 11:43 pm
by feyd
You don't need an infinite loop. For one, that'll eat the server's processor(s).
After removing the infinite loop, when data is added to the database run the script. Have the script save it's result data to a static file.
Posted: Fri Aug 18, 2006 11:44 pm
by Weirdan
that's inherent XML problem - it's not streamable.
Posted: Fri Aug 18, 2006 11:47 pm
by calmchess
I don't really have a reason for having a loop that doesn't end i'm just experimenting ....think i'll make chat using mysql,php,flash and xml......i'm just experimenting .....i'm new to programming.
Posted: Fri Aug 18, 2006 11:50 pm
by calmchess
here is the flash if anybody intrested there is a list object on the stage with a instance name of theList
Code: Select all
var theXML:XML = new XML();
theXML.ignoreWhite = true;
theXML.onLoad = function (){
var nodes = this.firstChild.childNodes;
for (i=0;i<nodes.length;i++){
theList.addItem(nodes[i].firstChild.nodeValue),i
}
}
theXML.load("http://localhost/webserver1/development/products2.php")
Posted: Mon Aug 21, 2006 2:44 pm
by christian_phpbeginner
Hi everybody, I don't know if it's worth to use the xml_writer built-in function in PHP than to create one by ourself like calmches did. Anybody has tried using the built-in function to write XML ??
http://id.php.net/manual/en/ref.xmlwriter.php
Thanks!
Posted: Tue Aug 22, 2006 12:17 am
by RobertGonzalez
I usually use a prebuilt (or modded prebuilt) script that uses some of the XML functions of PHP.