French Characters from MySQL database to PHP with XML eport fail

- 1 answer


I'm trying to doing and HTML query to an SQL database of mine using Javacript. The code I'm using using a PHP file to query the MySQL database for what I'm looking for then the PHP files exports the info to and XML file that my HTML page reads and creates data from.

The issue is that the export from the SQL database to and XML file causes an error when a French character is in play.

The error I get is:

<parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
<h3>This page contains the following errors:</h3>
<div style="font-family:monospace;font-size:12px">error on line 2 at column 1: Extra content at the end of the document
<h3>Below is a rendering of the page up to the first error.</h3>

This is on my local machine.. Before on my GoDaddy host it use to just cut off at the french character on export and wouldn't complete the export..

My PHP code is ..


//Get parameters from URL
$ne_lat = $_GET["nelat"];
$ne_lng = $_GET["nelng"];
$sw_lat = $_GET["swlat"];
$sw_lng = $_GET["swlng"];
$year = $_GET["year"];
$type = $_GET["type"];

$dom = new DOMDocument("1.0");
$node = $dom -> createElement("**");
$parnode = $dom -> appendChild($node);

//Open connection to mySQL server
$connection=mysql_connect ('localhost', '**', '**');
if (!$connection) {  die('Not connected : ' . mysql_error());}

// Set the active MySQL database
$db_selected = mysql_select_db('**', $connection);
if (!$db_selected) {
	die ('Can\'t use db : ' . mysql_error());

$query = "SELECT * FROM markers WHERE
(CASE WHEN $sw_lat < $ne_lat THEN lat BETWEEN $sw_lat AND $ne_lat ELSE lat BETWEEN $ne_lat AND $sw_lat END)
(CASE WHEN $sw_lng < $ne_lng THEN lng BETWEEN $sw_lng AND $ne_lng ELSE lng BETWEEN $ne_lng AND $sw_lng END)
($year >= startyear AND $year <= endyear)
(type IN $type)";

$results = mysql_query($query);
if (!$results) {
	die ("Invald query: " .mysql_error());

header("Content-type: text/xml; charset=iso-8859-1");

while ($row = @mysql_fetch_assoc($results)){
	$node = $dom->createElement("marker");
	$newnode = $parnode->appendChild($node);
	$newnode->setAttribute("description", $row["description"]);
	$newnode->setAttribute("lat", $row["lat"]);
	$newnode->setAttribute("lng", $row["lng"]);
	$newnode->setAttribute("type", $row["type"]);
	$newnode->setAttribute("year", $row["startyear"]);

echo $dom->saveXML();


Any suggestions?




Try adding this to your code just after your connection is initialized:

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $connection);

I hope it helps!