gmc1103 Posted October 25, 2023 Share Posted October 25, 2023 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 So any help what can it be?? Quote Link to comment https://forums.phpfreaks.com/topic/317393-mysql-save-utf-8-in-a-table-but-other-table-not/ Share on other sites More sharing options...
requinix Posted October 25, 2023 Share Posted October 25, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317393-mysql-save-utf-8-in-a-table-but-other-table-not/#findComment-1612553 Share on other sites More sharing options...
gmc1103 Posted October 25, 2023 Author Share Posted October 25, 2023 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/317393-mysql-save-utf-8-in-a-table-but-other-table-not/#findComment-1612554 Share on other sites More sharing options...
Strider64 Posted October 25, 2023 Share Posted October 25, 2023 If you are `editing` the data anywhere else and that data isn't clean then you will have to look at that code as well. I hope I'm making sense. Quote Link to comment https://forums.phpfreaks.com/topic/317393-mysql-save-utf-8-in-a-table-but-other-table-not/#findComment-1612555 Share on other sites More sharing options...
gmc1103 Posted October 25, 2023 Author Share Posted October 25, 2023 Yes, it seems to be the code since when i use WorkBench i'm able to correct the field to participação I'm going to double check with debug where is the problem Thanks all Quote Link to comment https://forums.phpfreaks.com/topic/317393-mysql-save-utf-8-in-a-table-but-other-table-not/#findComment-1612557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.