accent problem when saving to database [duplicate]

0

There is a problem with me, when I save a value in the database, it saves without any accent.

All my code on the page is utf8, my whole database is utf8, the tables are utf8 and where the insert is utf8

When I put utf8_encode () in the code, it works, but when I retrieve the value, it comes with the same accent problem

public function cadastrarResponsavel(){
        try{
            $sql = "INSERT INTO $this->prt_partner_responsible(id_resp,nome,cargo,tel_comercial1,tel_comercial2,tel_celular1,tel_celular2,email1,email2,nota,_id_partner,_ativo,_data_registro )
VALUES (NULL,:nome,:cargo,:tel_comercial1,:tel_comercial2,:tel_celular1,:tel_celular2,:email1,:email2,:nota,:id_partner,:ativo,:data_registro)";
            $stmt = DB::prepare($sql);


            foreach($this->nomeResponsavel as $key => $values) {

                $stmt->bindParam(":nome", $this->nomeResponsavel[$key], PDO::PARAM_STR);
                $stmt->bindParam(":cargo", $this->cargoResponsavel[$key], PDO::PARAM_STR);
                $stmt->bindParam(":tel_comercial1", $this->telefoneComercial1Resp[$key], PDO::PARAM_STR);
                $stmt->bindParam(":tel_comercial2", $this->telefoneComercial2Resp[$key], PDO::PARAM_STR);
                $stmt->bindParam(":tel_celular1", $this->telefoneCelularIIResp[$key], PDO::PARAM_STR);
                $stmt->bindParam(":tel_celular2", $this->telefoneCelularIIResp[$key], PDO::PARAM_STR);
                $stmt->bindParam(":email1", $this->email1Resp[$key], PDO::PARAM_STR);
                $stmt->bindParam(":email2", $this->emaiI2Resp[$key], PDO::PARAM_STR);
                $stmt->bindParam(":nota", $this->comentariosResp[$key], PDO::PARAM_STR);
                $stmt->bindParam(":id_partner", $this->idParceiro, PDO::PARAM_STR);
                $stmt->bindParam(":ativo", $this->ativo, PDO::PARAM_STR);
                $stmt->bindParam(":data_registro", $this->datas, PDO::PARAM_STR);
                $stmt->execute();
            }
            return $stmt->rowCount();
        }catch (PDOException $ex){
            $Exc = new ExceptionDatabase();
            date_default_timezone_set('America/Sao_Paulo');
            $dataRegistro = date("Y-m-d H:i:s");
            $this->Caminho = explode("/", $_SERVER['SCRIPT_NAME']);
            $this->arquivo = $this->Caminho[count($this->Caminho)-1];
            $this->arquivoLog = 'log/erros.txt';
            $this->erro =  $ex->getCode();
            $this->mensagem  = $ex->getMessage();

            $Exc->setTipoLog(enum::Error);
            $Exc->setTitleLog($ex->errorInfo);
            $Exc->setDescLog($ex->getMessage());
            $Exc->setDataRegistro($dataRegistro);
            $Exc->setArquivo($this->arquivo);
            $Exc->setArquivoLog($this->arquivoLog);
            $Exc->setErro($this->erro);
            $Exc->setMensagem($this->mensagem);

            $Exc->erro();
        }
    }

Itsavesinthebanklikethis

ConnectionQuery

BasicallyItriedtwoways

Settingutf8inconfigandcallingDBclass

header('Content-Type:text/html;charset=UTF-8');//jatinhaesseheaderutf8define('DB_HOST','hostqualquer');//hostdefine('DB_NAME','bancodedadosqualquer');//nomedoservidordefine('DB_USER','usuarioqualquer');//nomedousuariodefine('DB_PASS','senhaqualquer');//nomedasenhadefine('DB_CHARSET','charset=utf8"');  //codificação

and set it to the connection class

require_once 'config.php';
class DB{
    private static $instance;
    public static function getInstance()
    {
        if(!isset(self::$instance))
        {
            try
            {
                self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS,DB_CHARSET);
                //self::$instance->exec("set names utf8");
                self::$instance->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
            } catch (PDOException $e)
            {
                echo $e->getMessage();
            }
        }
        return self::$instance;
    }

    public static function prepare($sql){
        return self::getInstance()->prepare($sql);
    }
}

and also putting the exec straight, did not work

require_once 'config.php';
class DB{
    private static $instance;
    public static function getInstance()
    {
        if(!isset(self::$instance))
        {
            try
            {
                self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
                self::$instance->exec("set names utf8");
                self::$instance->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

            } catch (PDOException $e)
            {
                echo $e->getMessage();
            }
        }
        return self::$instance;
    }


    public static function prepare($sql){
        return self::getInstance()->prepare($sql);
    }
}

nor run

performsnormally,butmessesallencoding

    
asked by anonymous 01.06.2017 / 14:04

2 answers

0

Set the charset utf8 on the PDO connection.

Your connection string would look like this:

"mysql:host=$host;dbname=$db;charset=utf8"

If you are using an old version of PHP, this command is ignored, then you should set it via command like this:

$dbh = new PDO("mysql:$connstr",  $user, $password);
$dbh->exec("set names utf8");

Try to make this setting and see if it works out.

    
01.06.2017 / 14:22
0

Problem solved, the error was when I tried to send the data with utf8_encode, since I had passed everything to utf8

    
01.06.2017 / 19:40