troubleshooting Question

XML Ampersand Issue

Avatar of ambientsbs
ambientsbs asked on
3 Comments1 Solution332 ViewsLast Modified:

I've tried everything I can to get around this issue and I cannot get anything to work. I'm using PHP and SQL to return an XML file with various fields. One of those fields is a URL field which contains & signs within it. I've seen plenty of posts online that say to replace the & sign with & but my problem still persists.

Using this code:

while($row = mysql_fetch_array($rs)) {
$query2 = "SELECT recordID, Year, Make, Model, Price, City, State, Zip, URL FROM MainTable_VehicleDataPull WHERE Year BETWEEN '$startyear' AND '$endyear' AND Make LIKE '%$make%' AND Model LIKE '%$model%' AND City = '$row[city]' AND State = '$row[state]'";
$rs2 = mysql_query($query2);					
$numrows = mysql_num_rows($rs2);
$count = $count + $numrows;
while($result_array = mysql_fetch_assoc($rs2)) {
//loop through each key,value pair in row   
$xml .= '<';
$xml .= 'record ';
foreach($result_array as $key => $value)
$xml .= "$key =\"";
$xml .= "";
$xml .= $value;
$xml .="\" ";   

$xml .= "/>";

What's happening is that my XML is returning properly for every record except the ones which contain an & or &amp; within them. Those records start with hrecord rather than the <record that all of the correct ones start with like such:

<record recordID="21063" Year="1965" Make="Chevrolet" Model="Corvair Corsa Hard Top" Price="9750" City="Reno" State="NV" Zip="" URL="" />
hrecord recordID ="22904" Year ="1967" Make ="Chevrolet" Model ="Camaro - Custom Roadster ~ $110,000" Price ="$ 110,000" City ="Reno" State ="NV" Zip ="" URL ="" />


Does anyone have any ideas as to how I can fix this?? This data is for an app and I'm being told by the developer that using CDATA won't work with what he's doing if that's a potential solution. Thank you!
Technology Scientist

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros