1 Reply Latest reply on Nov 17, 2008 3:12 PM by Newsgroup_User

    XML problem/error

    SyKeSnr1
      Hello,
      Currently I'm creating an "XML-pricefeed" which contains all my actual data that a pricecomparison website can use.

      Code of my test XML:

      <?php

      header("Content-type: text/xml");

      $host = "localhost";
      $user = "#*$!X";
      $pass = "#*$!X";
      $database = "#*$!X";

      $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
      mysql_select_db($database, $linkID) or die("Could not find database.");

      $query = "SELECT * FROM product ORDER BY naam ASC";
      $resultID = mysql_query($query, $linkID) or die("Data not found.");

      $xml_output = "<?xml version=\"1.0\"?>\n";
      $xml_output .= "<entries>\n";

      for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
      $row = mysql_fetch_assoc($resultID);
      $xml_output .= "\t<entry>\n";
      $xml_output .= "\t\t<naam><![CDATA[" . $row['naam'] . "]]></naam>\n";
      $xml_output .= "\t\t<content><![CDATA[" . $row['content'] . "]]></content>\n";
      $xml_output .= "\t</entry>\n";
      }

      $xml_output .= "</entries>";

      echo $xml_output;

      ?>

      My output however is all messed up. For instance the output still shows the Cdata tag:

      <naam><![CDATA[Name of the product]]></naam>

      And like the first 100 rows return empty:

      <entry>
      <naam><![CDATA[]]></naam>
      <content><![CDATA[]]></content>
      </entry>
      <entry>
      <naam><![CDATA[]]></naam>
      <content><![CDATA[]]></content>
      </entry>
      <entry>
      <naam><![CDATA[]]></naam>
      <content><![CDATA[]]></content>
      </entry>
      <entry>
      <naam><![CDATA[]]></naam>
      <content><![CDATA[]]></content>
      </entry>
      <entry>
      etc etc.

      Can anyone assist me in this one?
        • 1. Re: XML problem/error
          Level 7
          SyKeSnr1 wrote:
          > My output however is all messed up. For instance the output still shows the
          > Cdata tag:
          >
          > <naam><![CDATA[Name of the product]]></naam>

          That's correct.

          > And like the first 100 rows return empty:

          What happens when you run the same query in phpMyAdmin? Perhaps the
          first 100 rows don't contain those values.

          BTW, the following is very inefficient:

          for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){

          It calculates mysql_num_rows() every time the loop runs. It's more
          efficient to do this:

          while ($row = mysql_fetch_assoc($resultID)) {
          // build the XML
          }

          --
          David Powers, Adobe Community Expert
          Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
          Author, "PHP Solutions" (friends of ED)
          http://foundationphp.com/