0

I have following code with which i am writing the xml files in php,But when i run this code,I get following error

SimpleXMLElement::asXML(): string is not in UTF-8

Code for generating xml is as follows

<?php

    //Create Database connection
  $mysqli = new mysqli('localhost', 'user', 'pass', 'dbnam');

   if(mysqli_connect_errno()) {
      echo "Connection Failed: " . mysqli_connect_errno();
      }
 $query='select Siteurl from tablename order by colname2 desc ';

    $result = mysqli_query($mysqli,$query);  

    //Create SimpleXMLElement object
    $xml = new SimpleXMLElement('<urls/>');

    //Add each column value a node of the XML object
    while($row = mysqli_fetch_array($result)) {
        $mydata = $xml->addChild('url');
        $mydata->loc=$row['Siteurl'];
    }

  mysqli_close($mysqli);
    //Create the XML file
    $fp = fopen("sitemaps/sitemap2.xml","wb");

    //Write the XML nodes
    fwrite($fp,$xml->asXML());

    //Close the database connection
    fclose($fp);

?>

How can i correct the error guys.Please help me

user3843585
  • 17
  • 1
  • 7

2 Answers2

0

Validate the encoding/charset of the mysql connection. It can be set on an active connection or configured in the database server configuration.

See: http://www.php.net/manual/en/mysqli.set-charset.php

ThW
  • 19,120
  • 3
  • 22
  • 44
  • I checked it,its returning utf8 on mysql connection,but why i m getting this error then – user3843585 Jul 21 '14 at 11:41
  • @user3843585: Not the connection is finally the cause, but the actual string. You still need to verify that on your own, setting the correct mysql connection character-set is a pre-condition (and not doing so a common error, but it's only one of the things that can go wrong). – hakre Jul 21 '14 at 18:30
  • k.k.corrected the error with function utf8_encode function – user3843585 Jul 22 '14 at 06:08
  • You did not FIX it. utf8_encode() is only a workaround in this case. – ThW Jul 22 '14 at 08:32
0

On at least one of these many assignments:

$mydata->loc = $row['Siteurl'];

is having $row['Siteurl'] a string that does not contain UTF-8 encoded data.

For all assignments, SimpleXMLElement expects data to be Unicode encoded as UTF-8.

However, SimpleXMLElement will only tell you the moment you use the asXML() method (in your case that is $xml->asXML()).

If you now want to find out which of the string-data you assigned is not valid UTF-8 you have to verify this on your own by checking each string before you assign it.

You should find some useful information how to do that in existing Q&A material here on site, for example:

Done that should shed more light if you validate each of those strings and output error information in case the expected encoding as implicit precondition for the assignment is not matched.

Mysql btw. won't bother to deliver invalid sequences if it doesn't know better, so not showing errors in that case, just trying best. You might want to tweak that explicitly or some data you get from the database might just be binary "encoded" and therefore ignored by the connection charset.

For a good summary of using true UTF-8 with Mysql, please see:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836