0

I am trying to make an xml file by fetching data from a pgsql database using php PDO. I am getting Undefined index error where in the code I tried to add to XML document node values fetched from database. How to correct it.

Here is my code:

 <?php

    require("Connection.php");

    function parseToXML($htmlStr)
     {
         $xmlStr=str_replace('<','&lt;',$htmlStr);
         $xmlStr=str_replace('>','&gt;',$xmlStr);
         $xmlStr=str_replace('"','&quot;',$xmlStr);
         $xmlStr=str_replace("'",'&#39;',$xmlStr);
         $xmlStr=str_replace("&",'&amp;',$xmlStr);
         return $xmlStr;
        }

         $stmt = $conn->prepare("SELECT id, name, address, lat, lng, type FROM markers");            // Select all the rows in the markers table
         $stmt->execute();
           $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
            if (!$result) {
             echo "Error: " . $e->getMessage();
             }
             //     header("Content-type: text/xml");

    // Start XML file, echo parent node
             echo "<?xml version='1.0' ?>";
             echo '<markers>';
             $ind=0;
    // Iterate through the rows, printing XML nodes for each
             while ($row = $stmt->fetchAll())
             {
      // Add to XML document node
             echo '<marker ';
             echo 'id="' . $row['id'] . '" ';
             echo 'name="' . parseToXML($row['name']) . '" ';
             echo 'address="' . parseToXML($row['address']) . '" ';
             echo 'lat="' . $row['lat'] . '" ';
             echo 'lng="' . $row['lng'] . '" ';
             echo 'type="' . $row['type'] . '" ';      
             echo '/>';
             $ind = $ind + 1;
             }

             // End XML file
             echo '</markers>';

    ?>

This line of the code I am not sure is doing what it is supposed to do i.e. to iterate through rows and print XML nodes for each.

   // Iterate through the rows, printing XML nodes for each
         while ($row = $stmt->fetchAll())
              {
  // Add to XML document node
         echo '<marker ';
         echo 'id="' . $row['id'] . '" ';
         echo 'name="' . parseToXML($row['name']) . '" ';
         echo 'address="' . parseToXML($row['address']) . '" ';
         echo 'lat="' . $row['lat'] . '" ';
         echo 'lng="' . $row['lng'] . '" ';
         echo 'type="' . $row['type'] . '" ';      
         echo '/>';
         $ind = $ind + 1;
         }

This is error msg:

Notice: Undefined index: id in C:\xampp\htdocs\maps\db2xml.php on line 36
id=""
Notice: Undefined index: name in C:\xampp\htdocs\maps\db2xml.php on line 37
name=""
Notice: Undefined index: address in C:\xampp\htdocs\maps\db2xml.php on line 38
address=""
Notice: Undefined index: lat in C:\xampp\htdocs\maps\db2xml.php on line 39
lat=""
Notice: Undefined index: lng in C:\xampp\htdocs\maps\db2xml.php on line 40
lng=""
Notice: Undefined index: type in C:\xampp\htdocs\maps\db2xml.php on line 41
type="" />

Ctrl+U gives this output

<?xml version='1.0' ?><markers><marker <br />
<b>Notice</b>:  Undefined index: id in <b>C:\xampp\htdocs\maps\db2xml.php</b> on line <b>49</b><br />
id="" <br />
<b>Notice</b>:  Undefined index: name in <b>C:\xampp\htdocs\maps\db2xml.php</b> on line <b>50</b><br />
name="" <br />
<b>Notice</b>:  Undefined index: address in <b>C:\xampp\htdocs\maps\db2xml.php</b> on line <b>51</b><br />
address="" <br />
<b>Notice</b>:  Undefined index: lat in <b>C:\xampp\htdocs\maps\db2xml.php</b> on line <b>52</b><br />
lat="" <br />
<b>Notice</b>:  Undefined index: lng in <b>C:\xampp\htdocs\maps\db2xml.php</b> on line <b>53</b><br />
lng="" <br />
<b>Notice</b>:  Undefined index: type in <b>C:\xampp\htdocs\maps\db2xml.php</b> on line <b>54</b><br />
type="" /></markers>
XCeptable
  • 1,247
  • 6
  • 25
  • 49

1 Answers1

1

Your current problem is caused by the fact that you get all rows at once:

while ($row = $stmt->fetchAll())

This will get you a multi-dimensional array containing all rows.

You need:

while ($row = $stmt->fetch())

Apart from that I would recommend using something like http://php.net/manual/en/intro.xmlwriter.php instead of manually constructing your xml.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Thank you very much. This solved the problem. But why echo is not showing any output while I open this php file in browser, while ctrl+U shows the output? – XCeptable Oct 22 '18 at 13:55
  • I will study the link you provided & I think psgl also has functions to generate xml, I will study those too. Because I am following this tutorial which uses this method that's why I used this way. https://developers.google.com/maps/documentation/javascript/mysql-to-maps#echoxml – XCeptable Oct 22 '18 at 13:57
  • @XCeptable You are seeing output below the notices. As you are probably looking to it from a browser, the browser will interpret the xml tags as tags, so you will not see them in the page, only in the source. – jeroen Oct 22 '18 at 14:00
  • 1
    OK, I un commented now: header("Content-type: text/xml"); so it start showing in the browser window too. Thank you for the kind help :) – XCeptable Oct 22 '18 at 14:03