PHP libraries JSON encoding error

PHP libraries JSON encoding error

AloneInTheDarkAloneInTheDark Posts: 30Questions: 7Answers: 0
edited October 2022 in Free community support

Code:

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;



$editor = Editor::inst( $db, 'edoc_document_request as document_request', 'id_edoc' )
->fields(
    Field::inst( 'document_request.id_edoc' )
    ,Field::inst( 'document_request.id_user' )
    ,Field::inst( 'document_request.tipo_di_richiesta' )
    ,Field::inst( 'document_request.descrizione' )
    ,Field::inst( 'document_request.descrizione_chiusura_forzata' )
    
    ,Field::inst( 'wp_users.display_name' )
        ->options( Options::inst()
            ->table( 'wp_users' )
            ->value( 'ID' )
            ->label( 'display_name' )
    )
    ,Field::inst( 'compiti_istituzionali.descrizione' )
        ->options( Options::inst()
            ->table( 'edoc_compiti_istituzionali' )
            ->value( 'id_compiti_istituzionale' )
            ->label( 'descrizione' )
    )
    ,Field::inst( 'campi_applicazione.descrizione' )
    ,Field::inst( 'document_request.file_dichiarazione_unicita' )->setFormatter( Format::ifEmpty( '' ) )
    ,Field::inst( 'document_request.file_preventivi' )
        ->setFormatter( Format::ifEmpty( '' ) 
    )
    ,Field::inst( 'document_request.file_fattura' )
        ->setFormatter( Format::ifEmpty( '' ) 
    )
    ,Field::inst( 'document_request.file_bozza_oda' )
        ->setFormatter( Format::ifEmpty( '' ) 
    )
    ,Field::inst( 'document_request.file_durc' )
        ->setFormatter( Format::ifEmpty( null ) )

        //->upload( Upload::inst( $path_srv. 'durc-__ID__.__EXTN__' )
        ->upload(Upload::inst( function ( $file, $id ) use ( $db ) {
            $extn = pathinfo( $file['name'], PATHINFO_EXTENSION );
            $id_edoc=$_REQUEST['id_edoc'];
            $protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === 0 ? 'https://' : 'http://';
            $url_srv=$protocol.$_SERVER['HTTP_HOST'];

            //recupero i dati per il vecchio file
            $record = $db->query( 'select', 'edoc_document_request' )
                    ->get( "file_durc", "DATE_FORMAT(edoc_document_request.data_creazione,'%Y/%m') as path_file")
                    ->where( 'id_edoc', $id_edoc ,'=' )
                    ->exec()
                    ->fetchAll();
            if ($record!=''){
                //cancello il vecchio file
                $file_durc=$record[0]['file_durc'];
                $file_durc=basename($file_durc); //recupero solo il nome del file
                unlink(SRV_PATH .$record[0]['path_file'] . "/" . $file_durc);
            }

            $filename='durc_'.$id_edoc.".".$extn;
            $new_path = SRV_PATH .$record[0]['path_file'] . "/" . $filename;
            move_uploaded_file( $file['tmp_name'], $new_path );
            $db->update(
                'edoc_document_request', 
                    array ( //[
                    "file_durc" => $url_srv . URL_PATH . $record[0]['path_file'] . "/" . $filename
                ), //],
                array ( "id_edoc" => $id_edoc )
            );
        })
        ->validator( Validate::fileSize( 500000, 'Il file non può superare 500K' ) )
        ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'rar', 'pdf', 'zip' ), "Puoi allegare pdf,zip,rar,png,jpg" ) )
    )
    

    ,Field::inst( 'document_request.id_status_richiesta' )
        ->options( Options::inst()
            ->table( 'edoc_stato_richiesta' )
            ->value( 'id_status' )
            ->label( 'descrizione' )
            ->where( function ($db) {
                $db->where( 'stato_per_ufficio_contabile', 'Y' );
            })
    )
    
    
    ,Field::inst( 'document_request.data_ultima_modifica' )
    ,Field::inst( 'document_request.data_creazione' )
    ,Field::inst( 'edoc_stato_richiesta.descrizione' )
)

->leftJoin( 'wp_users', 'wp_users.ID', '=', 'document_request.id_user' )
->leftJoin( 'edoc_compiti_istituzionali as compiti_istituzionali', 'compiti_istituzionali.id_compiti_istituzionale', '=', 'document_request.id_compito_istituzionale' )
->leftJoin( 'edoc_campi_applicazione as campi_applicazione', 'campi_applicazione.id_campo_applicazione', '=', 'document_request.id_campo_applicazione' )
->leftJoin( 'edoc_stato_richiesta', 'edoc_stato_richiesta.id_status', '=', "document_request.id_status_richiesta" )

->debug(true)
->process($_POST)
->json();

Description of problem:

Recive this: {"error":"JSON encoding error: Malformed UTF-8 characters, possibly incorrectly encoded"}
How can i set coding?

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • AloneInTheDarkAloneInTheDark Posts: 30Questions: 7Answers: 0

    Error on this string "pubblicazione articolo scientifico finalizzato a consentire la divulgazione dei risultati delle attività di ricerca"
    How can set the encoding?

  • AloneInTheDarkAloneInTheDark Posts: 30Questions: 7Answers: 0

    Resolution add this:

    $db->sql("SET character_set_client=utf8");
    $db->sql("SET character_set_connection=utf8");
    $db->sql("SET character_set_results=utf8");

  • LGC-AnthonyLGC-Anthony Posts: 1Questions: 0Answers: 0

    Thanks, this helped me.

    I had the same problem with french text and it's not a database problem, only datatables had trouble with the characters.

    I've put these lines in Editor.php for a global fix, not sure if it's really the correct place but it works for me.
    Line 135 in version 1.9.7

        function __construct($db = null, $table = null, $pkey = null)
        {
            // Set constructor parameters using the API - note that the get/set will
            // ignore null values if they are used (i.e. not passed in)
            $this->db($db);
            $this->table($table);
            $this->pkey($pkey);
    
            $db->sql("SET character_set_client=utf8");
            $db->sql("SET character_set_connection=utf8");
            $db->sql("SET character_set_results=utf8");
        }
    
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    It entirely depends on how the PHP / MySQL connection is set up and also on the database. Sometimes this is required, sometimes not. Unfortunately, I haven't yet found a way to automatically determine if it is needed or not!

    Note that you can also use $db->sql('set names utf8'); which is basically the same thing, just shorter.

    Allan

Sign In or Register to comment.