PHP Converts mysql database data into XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

PHP Converts mysql database data into XML

Post 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? 8O
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Erm... add/detect a top level tag and insert the data as a descendant.
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post 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&
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

For future reference, posting code always increases your chances of getting a knowledgable response.
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

Everah | Please use

Code: Select all

,

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

,

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]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What's with the

Code: Select all

while (1) {
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post by calmchess »

while (1) is how i make it loop forever
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

And why do a loop that doesn't end?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

that's inherent XML problem - it's not streamable.
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post 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.
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post 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")
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I usually use a prebuilt (or modded prebuilt) script that uses some of the XML functions of PHP.
Post Reply