Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql: SET NAMES utf8 on each connection?

Tags:

php

mysql

utf-8

I use the following statement on every PHP page to set the connection to Unicode:

mysql_query ('SET NAMES utf8');

Is there a way I can skip this? By setting something on the server? I have a VPS so I should be able to access anything.

I supposed the real question is: do I need to? Will it speed things up I skip this statement for every single HTML page that is delivered?

like image 698
soupagain Avatar asked Mar 09 '11 18:03

soupagain


3 Answers

you can modify mysql config(my.cnf)

[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8
like image 79
frail Avatar answered Oct 07 '22 05:10

frail


There's no harm and effectively zero performance impact by running that command whenever you start a new connection.

If you wish to change the server configuration, here is the appropriate manual page on setting the connection character set defaults. You'll want to read the rest of the sections on defaults as well.

like image 45
Charles Avatar answered Oct 07 '22 03:10

Charles


Why don't you just include a config file in every page, that contains all things related to creating a db connection?

This line is nesseccary, because you can really mess up data encoding in the db (been there, done that :))

like image 37
Mārtiņš Briedis Avatar answered Oct 07 '22 03:10

Mārtiņš Briedis