Jeff
Jeff

Reputation: 245

utf8 in database - set default_charset in php.ini

I have set utf8 in mysql database

now it shows

+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+


+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+

I have the below in the php code

mysqli_query("SET NAMES 'utf8'");

and

$con = mysqli_connect('host','user','pass','database');
mysqli_query($con,"SET NAMES 'utf8'");

Since I set utf8 in database, I am planning to remove

mysqli_query("SET NAMES 'utf8'");

and

mysqli_query($database,"SET NAMES 'utf8'");

from PHP code

I was told that I can remove the above code but I need to add

mysqli_set_charset($con,"utf8");

in the PHP code.

Instead of adding mysqli_set_charset($con,"utf8") in the code, can I set

default_charset = "UTF-8" 

in php.ini?

my.cnf is

[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_unicode_ci
init-connect='SET NAMES utf8'

Upvotes: 2

Views: 8663

Answers (2)

usman zafar
usman zafar

Reputation: 230

The question is in reference to MySQL so setting up a config in php.ini won't help much in this case. What i suggest is to update the my.cnf file in mysql

more about my.cnf settings can be found here

[client] 
default-character-set=utf8

[mysqld] 
character-set-server=utf8 
default-character-set=utf8 
default-collation=utf8_unicode_ci 
character-set-client = utf8

i took the above part from this thread, which discusses the same issue but for both PHP and MySQL. Hope this helps!

Upvotes: 0

C3roe
C3roe

Reputation: 96454

Instead of adding mysqli_set_charset($con,"utf8") in the code, can I set default_charset = "UTF-8" in php.ini?

No, that does not affect the database connection character set.

And even the MySQL extension settings offer nothing in that regard – and neither do those for MySQLi.

It is a per-connection setting after all.

But if you put your code to establish the database connection into one file that you include wherever it is needed, that should be enough.

Upvotes: 2

Related Questions