PHP does not send accents to Mysql database

Asked

Viewed 27,292 times

6

Good afternoon, you guys!

I installed the OCOMON 2.0-RC6 Help-Desk (Call Opening) system and am having the following problem:

PHP looks for the accented words of the database and displays them normally, this proves that the database accepts accents and 'ç' and PHP displays them. No problem. The point is that I can only insert accented or 'ç' words into the bank by phpmyadmin or mysql (cli). I can’t use the direct OCOMON system. There is some problem sending the data contained in the fields, that is, when PHP sends it to the database. If I type in description: "User contacted informing that his printer stopped printing." no data will be entered in the database in the'Description' field due to the accent of the word 'user'.

I’ve tried everything (change the collation - utf-8, latin1, iso-8859-1)... all right... help Please. Mysql 5.5.28 + PHP 5.4.3

  • If you use htmlentities or htmlespecialchars in the field before entering the query, you need to set the charset in the function parameters, check the manual http://br1.php.net/manual/en/function.htmlspecialchars.php

  • Did you solve this problem? Did any of the answers solve the problem?

  • Related: http://answall.com/a/43205/3635

  • None of these answers are correct?

8 answers

9

Try placing this line of code in the header of each page of your application and especially in the files that are processing the data to be entered in the database:
ini_set('default_charset', 'UTF-8');

If this doesn’t work, check the charset encoding of its connection to the database.
If you’ve been using MySQL:

$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

MySQLi:

$link = mysqli_connect("localhost","my_user","my_password","my_db");
mysqli_set_charset($link,"utf8");

PDO:

$db = new PDO(   'mysql:host='.DB_HOST.'; dbname='.DB_NAME, DB_USER, DB_PASS, 
            array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
                PDO::ATTR_PERSISTENT => false,
                PDO::ATTR_EMULATE_PREPARES => false,
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
            )
        );
  • My help was great, thank you!

  • That was the best answer!

4

What you really need is to unify the encoding being used.

Check your HTML encoding and use it in the database. If the site encoding is UTF-8, put UTF-8 in the database.

Also check that the application is not doing any conversion type before sending to the bank. Some libraries to bank do this.

2

A few steps are needed and I’ll show you here:

1 - Make sure your entire system structure is being encoded in a single charset: Example:

inserir a descrição da imagem aqui

2 - To make sure that everything will go well with the characters entered in the database, after the script connection to the database, I insert the following code:

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

And taking these precautions, it’s been a long time since I’ve had problems with accentuation doing any operation of CRUD in the bank of my websites and systems.

1

guy I researched on the sourceforge forum related to that project and already had a ticket on that subject.

look at this link http://sourceforge.net/p/ocomonphp/bugs/168/#1aa1

Edited

I took a look at the connection code of this system, which alias is kind of old(2005) described in the header of the file conecta.class.php (although the author updated the project until 2009 according to LEIAME.txt)

  1. locate the file "includes/classes/conecta.class.php" in the application’s root directory
  2. Now apply the code line $this->con=mysql_connect(SQL_SERVER,SQL_USER,SQL_PASSWD)or die(mysql_error());
  3. Below the line above, add mysql_set_charset("UTF8", $this->con);

OBS: replace UTF8 in function mysql_set_charset by the type of charset you want to work, I think utf8 on everything, including your html and database already solves the problem with accents.

1

Put the collation of the database ALWAYS as utf8_general_ci and in PHP code, when der Insert do so $nome = utf8_encode($_POST["nome"]);

I’ve always worn it like this, I’ve never had a problem ;)

  • This @Alisson-Acioli solution wouldn’t be exhausting and tiresome if there was a need to process dozens of variables?

  • 2

    @marcosvinicius yes, that’s true. I’ve always had problems with coding and found a solution like this. It’s the sacrifice I make.

  • I recommend my answer I think just below yours so that you no longer go through this suffocation and lost time. Forehead, will be solution for you.

0

Want to avoid problems with accents?

Create/convert all files (PHP, HTML, CSS, all) to UTF8 without BOM.

In html put the charset in utf8, in php using header content-type as utf8 and in the database specify at the time of connection charset utf8 by PDO or mysqli_set_charset.

You will never hear about charset problem again, alias this is a problem?

  • Sometimes this may not be an option, especially if the project is already in production environment...

  • 1

    It’s not a matter of choice, it’s a matter of doing it right so it doesn’t become a problem, because it’s not really a problem.

-1

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

This worked for me. However my site is all encoded in UTF-8 and the general database collation also.

Always put this code after including the database connection.

And convert all site pages to UTF-8 without GOOD (can do with Notepad++).

-1

I searched all over the internet and put this code in the connection file, indicated in this post resolved:

mysql_query("SET NAMES 'utf8'"); 
mysql_query('SET character_set_connection=utf8'); 
mysql_query('SET character_set_client=utf8'); 
mysql_query('SET character_set_results=utf8'); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.