By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,934 Members | 1,441 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,934 IT Pros & Developers. It's quick & easy.

Create XML with MySQL and PHP

P: 34
Hi there dose anyone know weather php can output XML
Zar
Jul 29 '07 #1
Share this Question
Share on Google+
10 Replies


kovik
Expert 100+
P: 1,044
Yes, yes it can. XML is just like HTML, but with different markup. Any more questions?
Jul 29 '07 #2

P: 34
Hi thanks I've create xml using MySQL and php, but i need to select paticular rows from my data base I'v try using WHERE in my query but this is not working can you help here is my code:
//
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("Content-type:text/xml");
  3. $con=mysql_connect("localhost","userrname","password")or die(mysql_error());
  4. mysql_select_db("database",$con);
  5. $query ="SELECT*FROM male WHERE Imagename ='$glog ORDER BY imagename ASC";
  6. $resultID=mysql_query($query, $con) or die("Data not found.");
  7. $xml_output="<?xml version=\"1.0\"?>\n";
  8. $xml_output.="<products title='Product'>\n";
  9. for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
  10. $row = mysql_fetch_assoc($resultID);
  11. $xml_output .= "\t<InSert itemdiscription='$row[name]'>\n";
  12. $xml_output .= "\t<id ident='$row[iddent]'></id>\n";
  13. $xml_output .= "\t<Size gs='$row[size]'></Size>\n";
  14. $xml_output .= "\t<Price gp='$row[price]'></Price>\n";
  15. $xml_output .= "\t<Quantity gq='$row[quantity]'></Quantity>\n";
  16. $xml_output .= "\t<Discription gd='$row[discription]'></Discription>\n";
  17. $xml_output .= "\t<Imagename gi='$row[imagename]'></Imagename>\n";
  18. $xml_output .= "\t</InSert>\n";
  19. }
  20. $xml_output .= "</products>";
  21. echo $xml_output; 
  22. ?>
kind regard Zar
Jul 31 '07 #3

ak1dnar
Expert 100+
P: 1,584
what should be the structure for your xml file? show it to us.
it is not clear enough for me in your coding.

if it is me,I'll create my XML elements like this.

Expand|Select|Wrap|Line Numbers
  1. $xml_output .= '<itemdiscription>'.$row['name'].'</itemdiscription>';
  2. // and Others here..
  3.  
Jul 31 '07 #4

P: 34
The code is for a flash site which has already been made so i have to reflect who the old XML code was whiten: here is the display. Hope you can help
Zar

<products title="Product">
<InSert itemdiscription="Raincoat">
<id ident="c600"/>
<Size gs="32"/>
<Price gp="47.00"/>
<Quantity gq="1"/>
<Discription gd="1960s Italian wool gabardine raincoat Lobster boy. Condition Ex"/>
<Imagename gi="children/childimages/April07001.jpg"/>
</InSert>
<InSert itemdiscription="Velvet waistcoat">
<id ident="00282"/>
<Size gs="27"/>
<Price gp="18.00"/>
<Quantity gq="1"/>
<Discription gd="1980s Velvet waistcoat. Condition Ex"/>
<Imagename gi="children/childimages/DSC00028.jpg"/>
</InSert>
<InSert itemdiscription="Poloneck jumper">
<id ident="00281"/>
<Size gs="26"/>
<Price gp="18.00"/>
<Quantity gq="1"/>
<Discription gd="1970s Poloneck jumper. Condition Ex"/>
<Imagename gi="children/childimages/DSC00028.jpg"/>
</InSert>
<InSert itemdiscription="Wool shawl">
<id ident="00283"/>
<Size gs="None"/>
<Price gp="9.00"/>
<Quantity gq="1"/>
<Discription gd="1960s Hand embroidered tyrol wool shawl. Condition Ex"/>
<Imagename gi="children/childimages/DSC00028.jpg"/>
</InSert>
</products>
Jul 31 '07 #5

ak1dnar
Expert 100+
P: 1,584
This will match with your out put.do the changes against to your database table.
I just removed some unwanted contents inside your while loop.

post back if there is doubt.

Thanks ! -ajaxrand


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("Content-type:text/xml");
  3. $con=mysql_connect("localhost","root","dba")or die(mysql_error());
  4. mysql_select_db("test",$con);
  5. $query ="SELECT * FROM products";
  6. $resultID=mysql_query($query, $con) or die("Data not found.");
  7. $xml_output="<?xml version=\"1.0\"?>\n";
  8. $xml_output.="<products>";
  9. for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
  10. $row = mysql_fetch_assoc($resultID);
  11. $xml_output .= '<InSert itemdiscription="'.$row['p_id'].'">';// ELEMENT STARTS
  12. $xml_output .= '<id ident="'.$row['p_name'].'"></id>'; // CHILDS 
  13. // REPEAT THE OTHER CHILDS HERE
  14.  
  15. $xml_output .= '</InSert>'; // ELEMENT END
  16. }
  17. $xml_output .= "</products>";
  18. echo $xml_output; 
  19. ?>
  20.  
Aug 1 '07 #6

ak1dnar
Expert 100+
P: 1,584
Did you Know?
How to use CODE tags arround your codings.

Answer is here
Aug 1 '07 #7

gregerly
Expert 100+
P: 192
Hi thanks I've create xml using MySQL and php, but i need to select paticular rows from my data base I'v try using WHERE in my query but this is not working can you help here is my code:
//
Expand|Select|Wrap|Line Numbers
  1. $query ="SELECT*FROM male WHERE Imagename ='$glog ORDER BY imagename ASC";
  2.  
I might try spacing things out in your query, don't know if this is exactly how you have it written in your code, but it seems cramped to me. You also have a single quote left out after the $glog variable. I would rewrite like this:

[PHP]$query = "SELECT * FROM male WHERE Imagename = '$glog' ORDER BY imagename ASC;"[/PHP]

Also, does the "male" table have two fields named imagename, one lower case and one upper case as you have it written in your query. If so you (I don't even thing you can do that in mysql), you may want to think about your table structures again. Also, your ordering by the same field specified in the Where clause, which means they are all going to be the same, thus you can't reorder them. Seems pointless to me....

But i've been wrong before!

Greg
Aug 1 '07 #8

ak1dnar
Expert 100+
P: 1,584
I might try spacing things out in your query, don't know if this is exactly how you have it written in your code, but it seems cramped to me. You also have a single quote left out after the $glog variable. I would rewrite like this:

[PHP]$query = "SELECT * FROM male WHERE Imagename = '$glog' ORDER BY imagename ASC;"[/PHP]
Greg,
you are absolutely correct. I think I also missed the single quote in the original post.

but, to the best of my knowledge this kind of MYSQL queries are executing perfectly.

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT*FROM products
  3.  
Aug 1 '07 #9

P: 34
Greg,
you are absolutely correct. I think I also missed the single quote in the original post.

but, to the best of my knowledge this kind of MYSQL queries are executing perfectly.

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT*FROM products
  3.  
HI thanks for your help, but I rewrote the flash file out and solved the problem
thanks for all your help
Zar
Aug 1 '07 #10

ak1dnar
Expert 100+
P: 1,584
HI thanks for your help, but I rewrote the flash file out and solved the problem
thanks for all your help
Zar
Glad to hear that,you got it working.Post back anytime if you have any doubts.!
-Ajaxand
Aug 1 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.