简体   繁体   中英

XML error 'not well formed'

i'm having a php file which returns an xml file and I'm getting "Not well formed" error because of the sentence: $name_sql= "SELECT * FROM product_description WHERE product_id = ".$id."";

The php file I initialised with:

header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');

include('config.php');

echo'<?xml version="1.0" encoding="utf-8"?>';

How can I rewrite that sentence? Thanks

<?php
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');

include('config.php');

error_reporting(E_ALL);


function getName($id) {

  $name_sql= "SELECT * FROM product_description WHERE product_id = ".$id."";

  $name_query = mysql_query($name_sql) or die('Eroare nume: '.mysql_error());
  $nume = mysql_fetch_array($name_query);
  return $nume;
}


echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<test>';

$var = mysql_query("SELECT * FROM product order by product_id ASC") or die(mysql_error());
while($var1 = mysql_fetch_array($var)) {
  $nume = getName($var1['product_id']);

  echo '<column><line>'.$nume['name'].'</line></column>';
}


echo '</test>';

<?php

$_CONFIG['db_host'] = 'xyz'; $_CONFIG['db_user'] = 'xyz'; $_CONFIG['db_pass'] = 'xyz'; $_CONFIG['db_name'] = 'xyz';

$_CONFIG['site_URL'] = 'http://xyz.ro';

$lnk = mysql_connect($_CONFIG['db_host'], $_CONFIG['db_user'], $_CONFIG['db_pass']) or die (mysql_error()); mysql_select_db($_CONFIG['db_name'], $lnk) or die (mysql_error());

?>

The first line in your XML must be: <?xml version="1.0" encoding="utf-8"?> but your include('config.php') might be outputting something before it.

Try something like this:

echo '<?xml version="1.0" encoding="utf-8"?>';
include('config.php');

function getName($id) {

  $name_sql= "SELECT * FROM product_description WHERE product_id = ".$id."";

  $name_query = mysql_query($name_sql) or die('Eroare nume: '.mysql_error());
  $nume = mysql_fetch_array($name_query);
  return $nume;
}
echo '<test>';

$var = mysql_query("SELECT * FROM product order by product_id ASC") or die(mysql_error());
while($var1 = mysql_fetch_array($var)) {
  $nume = getName($var1['product_id']);

  echo '<column><line>'.$nume['name'].'</line></column>';
}
echo '</test>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM