Jump to content

mysql save utf-8 in a table but other table not


gmc1103

Recommended Posts

Hello

 

I'm having a problem regarding saving data into database

I have 2 tables, one is keeping the character set defined, the other no

So, in one table i put "Participação" and it is saved as "Participaçã"

The other table i put "Participação" and it is saved as "Participação".

Both have the sames settings

image.png.b7ec95c23212229532a1f7783321319f.png

 

image.png.b40b32bfb9f45d2d48cc3dc956b64d88.png

 

So any help what can it  be??

Link to comment
Share on other sites

You're using the htmlentities function when you should not be.

1. You should be using htmlspecialchars instead when outputting something safely to HTML. htmlentities is unnecessary, and if you "need" it to make something look correct then that means you have other character encoding problems that need to be solved.
2. You should only be doing that at the very last moment right when you're doing the output - never before, or else you'll run into problems such as your current issue.

Link to comment
Share on other sites

27 minutes ago, requinix said:

You're using the htmlentities function when you should not be.

1. You should be using htmlspecialchars instead when outputting something safely to HTML. htmlentities is unnecessary, and if you "need" it to make something look correct then that means you have other character encoding problems that need to be solved.
2. You should only be doing that at the very last moment right when you're doing the output - never before, or else you'll run into problems such as your current issue.

Thanks for your reply

 

htmlentities where??

before saving to DB i'm sure the data is correct

$this->db->query('INSERT INTO tblatividades (atividade, objetivos, haveClasses, participantes, grupoAlvo, orcamento, dataAtividade, hora, local, idAnoEscolar, idProfessor, idProjeto, dataLancado, versao) 
                VALUES (:atividade, :objetivos, :haveClasses, :participantes, :grupoAlvo, :orcamento, :dataAtividade, :hora, :local, :idAnoEscolar, :idProfessor, :idProjeto, NOW(),:versao)');
                //Bind values
                $this->db->bind(':atividade', $data['atividade']);
                $this->db->bind(':objetivos', $data['objetivos']);
                $this->db->bind(':haveClasses', $data['haveClasses']);
                $this->db->bind(':participantes',$data['participantes']);
                $this->db->bind(':grupoAlvo', $data['grupoAlvo']);
                $this->db->bind(':orcamento', $data['orcamento']);
                $this->db->bind(':dataAtividade', $dataAtividade);
                $this->db->bind(':hora', $data['hora']);
                $this->db->bind(':local', $data['local']);
                $this->db->bind(':idAnoEscolar', $idAnoEscolar);
                $this->db->bind(':idProfessor', $data['idProfessor']);
                $this->db->bind(':idProjeto',$idProjeto);
                $this->db->bind(':versao', $versao);
                $turma = $data['idTurma'];
                $idProfessores = $data['idProfessores'];
                $idAtividade =$this->db->execute();

$this->db->bind(':atividade', $data['atividade']);

echo this, gives me "Participação"but saved as "Participaçã"

The character set is the same for both tables and the collation either, so i don't get why this is happening

 

This is my db connection

 

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
setlocale( LC_ALL, "pt_PT.UTF-8");

class Database {
    private $host = '';
    private $user = '';
    private $pass = '';
    private $dbname = '';
    private $charset = "";

    //Will be the PDO object
    private $dbh;
    private $stmt;
    private $error;


    public function __construct(){



        //Set DSN
        $dsn = "mysql:host=$this->host;dbname=$this->dbname;charset=utf8mb4";
        //Create PDO instance
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,ALLOW_INVALID_DATES"'));

        }catch(PDOException $e){
            $this->error = $e->getMessage();
            echo $this->error;
        }
    }

    //Prepare statement with query
    public function query($sql){
        $this->stmt = $this->dbh->prepare($sql);
    }

    //Bind values, to prepared statement using named parameters
    public function bind($param, $value, $type = null){
        if(is_null($type)){
            switch(true){
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        //echo $this->stmt->bindValue($param, $value, $type);
        $this->stmt->bindParam($param, $value, $type);
    }

    //Execute the prepared statement
    public function execute(){
        $info = $this->stmt->execute();
        $id = $this->lastIdInserted();
        if($id != 0){
            $info = $id;
        }
        else{
            if($info){
                $info = true;
            }
            else{
                $info = false;
            }
        }
        return $info;
    }

    //Return multiple records
    public function resultSet(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    //Return a single record
    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    //Get row count
    public function rowCount(){
        return $this->stmt->rowCount();
    }

    public function lastIdInserted(){
        return $this->dbh->lastInsertId();
    }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.