1

I'm having some errors in my MySQL with accents, all my files are in UTF-8.

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

But still not work, it just work if I place this after the MySQL Connection:

mysqli_query($conn,'SET character_set_results=utf8');

I have multiples functions that starts a new connection, so need to add for every one, its a problem.

My database are all in UTF-8 format.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • what's the error? and where is the rest of your code? – Funk Forty Niner Oct 02 '18 at 01:45
  • setup a function for it and pass the connection to it as an additional parameter. Or look into using OOP. You'd have to show us what those functions are and how you're including all these files, if there are many. The question's unclear for me. – Funk Forty Niner Oct 02 '18 at 01:46
  • 4
    Possible duplicate of [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – miken32 Oct 02 '18 at 01:52
  • The error that appears ? instead of tildes like Vélcis it appears V?lcis – user3811313 Oct 02 '18 at 01:58
  • that's not an error, it's just not showing you what it should be due to a few possible reasons that can be found in the link that was left above about the duplicate. Read through that and you might very well find your solution. @user3811313 – Funk Forty Niner Oct 02 '18 at 02:13

2 Answers2

2

After your mysqli_connect line use mysqli_set_charset($conn,"utf8"); and it will set to to every of yours connection.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30
  • The problem is, each function have a new mysqli_connect() so many things to edit. – user3811313 Oct 02 '18 at 03:01
  • Are all your $conn the same host, user, password, and database? – Jonathan Gagne Oct 02 '18 at 03:22
  • Yes, but it doesn't work outside class connection, than I need to set every class one connection. – user3811313 Oct 02 '18 at 04:57
  • Controle find + replace would take 30 seconds right? – Jonathan Gagne Oct 02 '18 at 06:48
  • unfortunately not, instead of classes a lot of files have $conn inside. My DB is already on UTF8, all my files stills UTF8 and nothing :/ – user3811313 Oct 02 '18 at 13:15
  • You will need to either refactor your code so that every place that now has it's own `mysqli_connect()` calls the same function that creates the connection and sets the charset or manually add it after every `mysqli_connect()`. But to make maintainance easier in the future you really should refactor this. – VaaChar Oct 02 '18 at 13:28
  • Each _connection_, not each _function_. Connect _only once_ per html page (or whatever unit of operation); pass $conn into all your functions (or make it `global`. – Rick James Oct 03 '18 at 03:26
0

try mysqli_set_charset($conn,'utf8')

$conn = mysqli_connect("host","username","password") or 
die(mysqli_error($conn));
mysqli_set_charset($conn,'utf8');
mysqli_select_db($conn,'databasename') or die("cannot select DB");
kazz
  • 3
  • 1
  • As I said, this is the only way to work, but I have one new connection in each class of my app, so need to add one by one. :x – user3811313 Oct 02 '18 at 05:00