error on line 3 at column 1: Extra content at the end of the document - XML

<?php
include "config.php";
if( ! ini_get('date.timezone') ){
    date_default_timezone_set('GMT');
}

/* require the user as the parameter */
if(isset($_GET['country'])) {
	
	/* soak in the passed variable or set our own */
	//$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
	$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
	//$user_id = intval($_GET['user']); //no default

	$country = $_GET['country'];
	//$LastSyncDate = "2015-03-05 17:00:00.000";

	/* connect to the db */
	// $link = odbc_connect ('dms_production','sysprogress','sysprogress') or die('Cannot connect to ODBC data source.');
	
	/* grab the posts from the db */
	$query = "SELECT * FROM customers WHERE country='$country'";
					  
	$query = trim($query);
	$result =mysqli_query($con,$query);

	/* create one master array of the records */
	$table = array();
	if(mysqli_num_rows($result)) {
		while($record = mysqli_fetch_array($result)) {
			
			$table[] = array('record' => $record);
		    //echo array('record' => $record);

			//query for reservation details


			// $reservationdetails = "SELECT * FROM pub.sadtl WHERE id_sahdr = '".$record['id_sahdr']."'";	
			
			// $detailsresult = odbc_exec($link, TRIM($reservationdetails)) or die('Query error:  '.TRIM($reservationdetails));
			// if(odbc_num_rows($detailsresult)){
			// 	while($res = odbc_fetch_array($detailsresult)){
			// 		$table[] = array('record' => $record, 'details' => $res);
			// 	}
			
			
		}
	}

	/* output in necessary format */
	if($format == 'json') {
		header('Content-type: application/json');
		//echo json_encode($table);
		echo json_encode($table);
	}
	else {
		header('Content-type: text/xml');
		echo '<table>';
		foreach($table as $index => $record) {
			if(is_array($record)) { 
				foreach($record as $key => $value) {
					echo '<',$key,'>'."aferfwefawfawefa";
					if(is_array($value)) {
						foreach($value as $tag => $val) {
							echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
						}
					}
					echo '</',$key,'>';
				}
			}
		}
		echo '</table>';
	}

	/* disconnect from the db */
	@mysql_close($link);
}
else {
  echo '<h3>Error: No passed parameters.<br>';
        
}
?>

Open in new window

jun lastmoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Jun, this is not very helpful. The error message in your question title has nothing to do with the code that you've posted. Your code has a few things wrong with it:

1. Vulnerable to SQL injection. If you don't know what that is, then read this:
https://www.experts-exchange.com/articles/1263/5-Steps-to-Securing-Your-Web-Application.html

2. On line 65, you use commas instead of periods for concatenation:
echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
...should be:
echo '<.$tag.'>'.htmlentities($val).'</.$tag.'>';

The same problem is on line 68.

Technically, this is not an ERROR (it -will- work), but it's not the correct way to do concatenation. What this is doing is passing multiple parameters to echo, and echo is stringing them together for you. It's just bad practice, though - if you want to do concatenation, use periods.

3. You don't send an XML declaration line, which is usually the first line in XML output. A common one is this:
<?xml version="1.0" encoding="UTF-8" ?>

4. You're mixing and matching mysql and mysqli. For example, line 76 should be mysqli_close, not mysql_close. Stick with mysqli.

5. This line:
echo '<',$key,'>'."aferfwefawfawefa";
...seems incorrect. If you have a record that has an array value, then you'll end up with XML like this:
<yourkey>aferfwefawfawefa
  <sometag>valueX</sometag>
  <othertag>valueY</othertag>
</yourkey>

Open in new window

...which is not valid XML. Take out the "aferfwefawfawefa" text if you don't have a good reason for it to be there.Or if it needs to be there, then make sure it doesn't appear when there's an array of values.

Finally, your error message sounds like an error while trying to READ the XML file, while your code shows us the CREATION of XML. If you want help with the resulting XML, make sure you provide a sample of the generated XML as well as the code that is trying to read it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.