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('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$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>