server_processing.php returns Malformed UTF-8 characters

server_processing.php returns Malformed UTF-8 characters

asleasle Posts: 96Questions: 28Answers: 0
edited November 2019 in Free community support

DataTables 1.10.19 and jQuery 1.12.4.
I am using the standard server_processing.php to fetch data. I have 3 other solutions using this script in different ways and have success with this. But now I setup this new database and like the other solutions the DB is innoDB and utf8_ci_general. I have even encoded every field as utf8mb4_general_ci. I have Norwegian characters in this table just like my other 3 solutions that work fine. But I get no data to the table and console shows: Uncaught TypeError: Cannot read property 'error' of null. This must be because I have errors in server_processing.php. When I add "echo json_last_error_msg();" to server_processing.php and run it directly it shows Malformed UTF-8 characters, possibly incorrectly encoded. If I run a var_dump() of the array I can see that there are malformed UTF-8 characters. But how do I fix this? I did not have to fix my other solutions for any UTF-8 problems.

As I pointed out I have not edited anything exept the connection details and the fields. Here is my server_processing.php:

$table = 'my_table';
$primaryKey = 'id';
$columns = array(
        array( 'db' => 'Latinsknavn',   'dt'     => 0 ),
        array( 'db' => 'Norsknavn', 'dt' => 1 ),
        array( 'db' => 'Familienorsk',  'dt' => 2 ),
        array( 'db' => 'Familielatin', 'dt' => 3 ),
);

// SQL server connection information
$sql_details = array(
    'user' => 'user_name',
    'pass' => 'pAss_WoRd',
    'db'   => 'data_base_name',
    'host' => 'localhost'
);
require( 'ssp.class.php' );
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

This question has an accepted answers - jump to answer

Answers

  • asleasle Posts: 96Questions: 28Answers: 0

    On my other solutions with the same server_processing.php I get e.g. "Løk" I get output--> L\u00f8k
    On this new setup I get for the same word --> L�k

  • asleasle Posts: 96Questions: 28Answers: 0
    edited November 2019

    Update

    I get the correct data when i changed the code in spss.class.php for the DB connection.

      // old code  
      //array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
      // new code
        array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'" )
    

    This seems to fix the problem and I get the data. Question is why I had to do this since it has worked before with my other databases without the change. I just want to understand :-)

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin
    Answer ✓

    Yes, I was going to suggest using SET NAMES utf8. It sounds like the PHP / MySQL connection configuration is not defaulting to UTF8, which required you to use the SET NAMES query to make sure it does.

    Allan

This discussion has been closed.