(PHP 4 >= 4.3.0, PHP 5)
mysql_client_encoding — 返回字符集的名称
$link_identifier
] )从 MySQL 中取得 character_set 变量的值。
link_identifier
MySQL
连接。如不指定连接标识,则使用由 mysql_connect()
最近打开的连接。如果没有找到该连接,会尝试不带参数调用
mysql_connect()
来创建。如没有找到连接或无法建立连接,则会生成
E_WARNING
级别的错误。
返回当前连接的默认字符集名称。
Example #1 mysql_client_encoding() 例子
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$charset = mysql_client_encoding($link);
echo "The current character set is: $charset\n";
?>
以上例程的输出类似于:
The current character set is: latin1
Note:
本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除。应使用 MySQLi 或 PDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南以及相关 FAQ 以获取更多信息。用以替代本函数的有:
- mysqli_character_set_name()
- PDO::setAttribute() (e.g., $db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");)
devos at montp dot inserm dot fr (2007-08-23 03:30:12)
I use mysql 3.23 and none of the above worked for me. Looking at http://dev.mysql.com/tech-resources/articles/4.1/unicode.html as advised by romain at dardour dot com, I tried to include the following header in my php scripts, and all the funy characters disapeared. Hope it will help.
<?php header("Content-type: text/html; charset=latin1");?>
(my mysql character was latin1)
php at quarkus dot de (2007-07-25 06:23:47)
Please note that even if you set the charset by issuing the two mentioned SQL statements (set names, set character set) mysql_client_encoding still deliveres the old result.
Default for me is latin1. After switching to UTF8 mysql_client_encoding still returns latin1. The charset switched to UTF8 successfully, though.
msieniek at interia dot pl (2007-07-03 09:31:10)
I had the same problem as bartullus, I'm from Poland too. I had my base encoded in UTF-8 and my website in ISO-8859-2 aka Latin-2. Long time those fuckin' question marks appeared...
I used this function mysql_client_encoding() but it shows "latin1" which appeared to be false. My solution was to include those two lines after connection to database in my PHP script:
<?
mysql_query("SET CHARACTER SET latin2");
mysql_query("SET NAMES latin2");
?>
So, probably this function lies;)
GZ (2007-03-18 06:14:24)
This one also may be necessary for happy work with utf-8, if you have no other encodings.
In server section add:
init-connect='SET NAMES utf8'
(from MySQL manual)
roy dot the dot geek+php at gmail dot com (2007-01-23 05:37:17)
f you think "set names utf8" for each connection is too trouble, you can modify my.cnf of MySQL to solve the problem forever. In my.cnf, add the line "default-character-set=utf8" in both [mysqld] and [client] sections:
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
The MySQL will use utf8 after you restart it.
bartullus at wp dot pl (2006-12-08 06:35:09)
For some reason given entry in php.ini:
default_charset = "ISO-8859-2"
nor
default_charset = "latin2"
doesn't work.
Every time you run:
$charset= mysql_client_encoding($dbh);
echo $charset;
from PHP connecting to MySQL you will get 'latin1'.
Even if all HTML/PHP pages are stored as UTF8 with header:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
and MySQL server is everywhere UTF8:
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
adding "SET NAMES latin2" and "SET CHARACTER SET latin2" into your PHP to MySQL connection you will always get question marks after doing select.
I also tried all the tips from this page and did not succeed as others stated 'it worked for me' - blah...spent 6 hours on that.
THE ONLY solution that "worked for me" and for others using latin2-iso-8859-2 (i'm from Poland) is to put single line into your SQL script with many inserts:
set name LATIN1;
After that, you can run a SQL script from QUERY BROWSER with the latin2 characters in inserts.
After only that, when you query those fields from PHP page via select you will get NO question marks and garbage.
If you use 'mysql' command line tool instead of QUERY BROWSER to run insert script you must remove "set names latin1" from the beginning of the script because of the error while processing that line. Instead of that run "set names latin1" separatelly and then run the insert script.
'mysql' works fine with "set names latin2" but only with singleline, manual inserts.
'mysql' command client works fine after switching to "set names latin2" but MySQL Query Browser do not. Query Browser v.1.2.6 beta is not great for some other reasons not worth mentioning here.
My configuration is:
Windows 2000
Apache2.2
MySQL 5.0.27
MySQL client shows 5.1.11
PHP5.2.0
and I needed to run insert script to fullfil dictionaries with initial data.
Pedro Pereira (2006-09-09 07:30:46)
All I had to do to save utf8 data with php mysql_query() was to go to the php.ini and put default_charset = "utf-8". Without this I had the same problems some of you have. Plus, all my mysql charsets vars are in 'utf8'. (Changed them with Mysql Admin Tool)
Didnt use any mysql SET **** command at all.
Mysql 4.1.20
PHP 4.4.4
Win XP
Hope this help some of you.
alamakota at mises dot pl (2006-07-26 07:13:51)
There's no 'character_set' variable available in MySQL. You can check it yourself in MySQL online documentation or by running MySQL query "SHOW VARIABLES LIKE 'character_set%';".
This must be an error in PHP manual, unless I'm missing something.
(2006-07-21 05:46:29)
A very easy way to always get results in UTF-8 is to make a common function to connect to DB and in that function set character set to utf-8.
function OpenConn() {
global $link;
$link = mysql_connect ("localhost", "username", "password")
or die(sendError('Could not connect to DB'));
mysql_select_db ("dbName");
//
// Set character set to UTF-8
//
mysql_query("SET CHARACTER SET 'utf8'", $link);
}
romain at dardour dot com (2006-05-15 08:56:04)
I couldn't get any luck with all the stuff mentioned below, and despite having an unicode DB, and setting all my field to utf8_general_ci...
After looking around, I found that this page: http://dev.mysql.com/tech-resources/articles/4.1/unicode.html was adding the fields with an extra info before each value:
<?
mysql_query("INSERT INTO table SET field = _utf8'value'");
?>
Mind the "_utf8" before the field value, and outside of the quotes.
This works for me wether in an Insert or an Update statement.
No need here for a <? mysql_query("SET NAMES utf8"); ?> before each query, or to change anything in the config files (that was important since I don't have access to these).
zayfod at yahoo dot com (2006-04-21 04:57:23)
The right lines to put in /etc/my.cnf (or other MySQL options file) are:
[client]
init-command="SET NAMES utf8"
Unfortuantely the PHP mysql_connect() function does not use MySQL options files so this is not a sollution for changing the default connection character set for mysqlclient library v4.1+.
The only working sollution remains:
mysql_query("SET NAMES utf8", $conn);
(of course /ext/mysql/php_mysql.c can always be patched ;] )
Wio at psitrax dot de (2006-03-16 09:24:23)
If you set the encoding in my.cnf like
[mysqld]
init_connect='SET NAMES utf8'
note that the content of init_connect is not executed for users that have the SUPER privilege - ie root!
vrbcik (2005-11-27 09:26:29)
I have had problems with encoding after export of tables (from hosting - via PhpMyAdmin) and import them to other machine (my notebook - via PhpMyAdmin too). In PhpMyAdmin the encoding of all data was shown correctly, not that good with the web pages (data pulled via php).
The first point is indication, that the data was imported correctly, but php script has got other character set than MySql is sending.
The script's character set is set in header: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">. MySql setting was latin1.
Then following code helped me:
<?php
mysql_query("SET CHARACTER SET 'latin2'", $conn);
?>
info at jantichy dot cz (2004-12-30 14:02:34)
Notice the difference between following two SQL statements:
SET NAMES 'charset_name'
SET CHARACTER SET charset_name
For more detail see
http://dev.mysql.com/doc/mysql/en/Charset-connection.html
black at scene-si dot org (2004-10-20 04:04:54)
the above (as it seems to be vaguely indicated in the mysql manual) works only with mysql 4.1+
stian at grytoyr dot net (2004-10-19 06:09:06)
If you experience weird problems, like some UTF-8 characters (the Unicode character č and a few others in my case) seemingly being changed to garbage by mysql_query, you may need to do something like this before your actual query:
<?php
mysql_query("SET NAMES 'utf8'", $conn);
?>
Took me days to figure that one out...