Server-Error: "Invalid UTF-8 sequence" server_processing.php on line 163

Server-Error: "Invalid UTF-8 sequence" server_processing.php on line 163

RockbRockb Posts: 97Questions: 0Answers: 0
edited March 2011 in Bug reports
Hello, actually the table works fine. But my server-error-log says:

[Wed Mar 02 14:50:17 2011] [error] [client xyz] PHP Warning: json_encode() [[a href='function.json-encode']function.json-encode[/a]]: Invalid UTF-8 sequence in argument in /xyz/server_processing.php on line 163, referer: http://xyz.com

Any idea, where the problem is?

Replies

  • RockbRockb Posts: 97Questions: 0Answers: 0
    edited March 2011
    I think it has to do with on of my aoColumns-Entries:

    {"fnRender": function ( oObj ) { return '' + oObj.aData[2] +''; }, "aTargets": [ 2 ]},

    The strange thing: It works, but the server error logs are not happy about it. Why?
  • allanallan Posts: 61,741Questions: 1Answers: 10,111 Site admin
    This was the first hit on Google when I did a quick search for the problem you are seeing: http://stackoverflow.com/questions/1790425/invalid-php-json-encoding . Sounds like a character set issue rather than a bug in DataTables :-)

    Allan
  • RockbRockb Posts: 97Questions: 0Answers: 0
    A thx, I'm not quite familiar with JavaScript, it's more try&error to me ;-)

    In this answer there is also the other answer for this topic here, or? http://stackoverflow.com/questions/1790425 /invalid-php-json-encoding/1790454#1790454 BUT WHERE is the json output, where I'Ve to add this line?
  • allanallan Posts: 61,741Questions: 1Answers: 10,111 Site admin
    The JSON is output on line 176 (second last line): http://www.datatables.net/development/server-side/php_mysql . So you can put the header command anywhere before that.

    Allan
  • RockbRockb Posts: 97Questions: 0Answers: 0
    Thank you for you reply, but there is no change with

    header('Content-Type: application/json; charset=utf-8', true,200);
    echo json_encode( $output );

    still the same server error.
  • allanallan Posts: 61,741Questions: 1Answers: 10,111 Site admin
    What is the character set of your database? Is it UTF8 or Latin1 or something else? You want to use the same character set everywhere, otherwise you'll need to use iconv or similar to convert between them.

    Allan
  • RockbRockb Posts: 97Questions: 0Answers: 0
    Everywhere it's UTF-8... database, website (that includes the table), even in the javascript (
  • RockbRockb Posts: 97Questions: 0Answers: 0
    May you could take a look? http://radiocharts.xn--rockbr-fua.de
  • allanallan Posts: 61,741Questions: 1Answers: 10,111 Site admin
    Do you use phppgadmin or phpmyadmin or anything like that? You will need to use a database tool (or query it directly yourself) to find out what character set it is using for your tables. Unfortunately I can't tell you what the issue is from just a web-page as it sounds like a database issue. This post might also offer a solution: http://datatables.net/forums/comments.php?DiscussionID=675#Item_4

    Allan
  • RockbRockb Posts: 97Questions: 0Answers: 0
    Indeed, that was it. The problem (if at all) was, my database and the rest of my website is coded in UTF-8, but the json-reader need to be informed to.

    So, I replaced the following part (line 127 in http://www.datatables.net/development/server-side/php_mysql):

    [code]$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());[/code]

    with

    [code]mysql_query("SET character_set_results=utf8", $gaSql['link']);
    $rResult = mysql_query( $sQuery ) or die(mysql_error());[/code]

    Thanx, Allan! :-) You're fantastic.
  • allanallan Posts: 61,741Questions: 1Answers: 10,111 Site admin
    Good stuff. So I think if you were to look at your database with a graphical viewer it will probably show that it is using latin1 or something else - which will break utf8 (since they aren't compatible). Usually best to try and work in just one character set for reasons you are seeing :-)

    Allan
  • RockbRockb Posts: 97Questions: 0Answers: 0
    No, even the database is completly in utf-8. But anyway, with your hint and change it works :-)
This discussion has been closed.