Create XML with MySQL and PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zarwadi
    New Member
    • Jun 2007
    • 34

    Create XML with MySQL and PHP

    Hi there dose anyone know weather php can output XML
    Zar
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    Yes, yes it can. XML is just like HTML, but with different markup. Any more questions?

    Comment

    • Zarwadi
      New Member
      • Jun 2007
      • 34

      #3
      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:
      //
      [code=php]<?php
      header("Content-type:text/xml");
      $con=mysql_conn ect("localhost" ,"userrname","p assword")or die(mysql_error ());
      mysql_select_db ("database",$co n);
      $query ="SELECT*FRO M male WHERE Imagename ='$glog ORDER BY imagename ASC";
      $resultID=mysql _query($query, $con) or die("Data not found.");
      $xml_output="<? xml version=\"1.0\" ?>\n";
      $xml_output.="< products title='Product' >\n";
      for($x = 0 ; $x < mysql_num_rows( $resultID) ; $x++){
      $row = mysql_fetch_ass oc($resultID);
      $xml_output .= "\t<InSert itemdiscription ='$row[name]'>\n";
      $xml_output .= "\t<id ident='$row[iddent]'></id>\n";
      $xml_output .= "\t<Size gs='$row[size]'></Size>\n";
      $xml_output .= "\t<Price gp='$row[price]'></Price>\n";
      $xml_output .= "\t<Quantit y gq='$row[quantity]'></Quantity>\n";
      $xml_output .= "\t<Discrip tion gd='$row[discription]'></Discription>\n" ;
      $xml_output .= "\t<Imagena me gi='$row[imagename]'></Imagename>\n";
      $xml_output .= "\t</InSert>\n";
      }
      $xml_output .= "</products>";
      echo $xml_output;
      ?>[/code]
      kind regard Zar
      Last edited by ak1dnar; Jul 31 '07, 03:32 PM. Reason: added Code tags

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        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.

        [code=php]
        $xml_output .= '<itemdiscripti on>'.$row['name'].'</itemdiscription >';
        // and Others here..
        [/code]

        Comment

        • Zarwadi
          New Member
          • Jun 2007
          • 34

          #5
          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>

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            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


            [code=php]
            <?php
            header("Content-type:text/xml");
            $con=mysql_conn ect("localhost" ,"root","dba")o r die(mysql_error ());
            mysql_select_db ("test",$con );
            $query ="SELECT * FROM products";
            $resultID=mysql _query($query, $con) or die("Data not found.");
            $xml_output="<? xml version=\"1.0\" ?>\n";
            $xml_output.="< products>";
            for($x = 0 ; $x < mysql_num_rows( $resultID) ; $x++){
            $row = mysql_fetch_ass oc($resultID);
            $xml_output .= '<InSert itemdiscription ="'.$row['p_id'].'">';// ELEMENT STARTS
            $xml_output .= '<id ident="'.$row['p_name'].'"></id>'; // CHILDS
            // REPEAT THE OTHER CHILDS HERE

            $xml_output .= '</InSert>'; // ELEMENT END
            }
            $xml_output .= "</products>";
            echo $xml_output;
            ?>
            [/code]

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Did you Know?
              How to use CODE tags arround your codings.

              Answer is here

              Comment

              • gregerly
                Recognized Expert New Member
                • Sep 2006
                • 192

                #8
                Originally posted by Zarwadi
                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:
                //
                [code=php]$query ="SELECT*FRO M male WHERE Imagename ='$glog ORDER BY imagename ASC";
                [/code]
                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

                Comment

                • ak1dnar
                  Recognized Expert Top Contributor
                  • Jan 2007
                  • 1584

                  #9
                  Originally posted by gregerly
                  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.

                  [code=sql]
                  SELECT*FROM products
                  [/code]

                  Comment

                  • Zarwadi
                    New Member
                    • Jun 2007
                    • 34

                    #10
                    Originally posted by ajaxrand
                    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.

                    [code=sql]
                    SELECT*FROM products
                    [/code]
                    HI thanks for your help, but I rewrote the flash file out and solved the problem
                    thanks for all your help
                    Zar

                    Comment

                    • ak1dnar
                      Recognized Expert Top Contributor
                      • Jan 2007
                      • 1584

                      #11
                      Originally posted by Zarwadi
                      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

                      Comment

                      Working...