mysql_client_encoding
(PHP 4 >= 4.3.0, PHP 5)
mysql_client_encoding — Returns the name of the character set
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
Description
$link_identifier
= NULL
] )Retrieves the character_set variable from MySQL.
Parameters
-
link_identifier
-
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an
E_WARNING
level error is generated.
Return Values
Returns the default character set name for the current connection.
Examples
Example #1 mysql_client_encoding() example
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$charset = mysql_client_encoding($link);
echo "The current character set is: $charset\n";
?>
The above example will output something similar to:
The current character set is: latin1
See Also
- mysql_set_charset() - Sets the client character set
- mysql_real_escape_string() - Escapes special characters in a string for use in an SQL statement
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MySQL Drivers and Plugins
- Оригинальное API MySQL
- mysql_affected_rows
- mysql_client_encoding
- mysql_close
- mysql_connect
- mysql_create_db
- mysql_data_seek
- mysql_db_name
- mysql_db_query
- mysql_drop_db
- mysql_errno
- mysql_error
- mysql_escape_string
- mysql_fetch_array
- mysql_fetch_assoc
- mysql_fetch_field
- mysql_fetch_lengths
- mysql_fetch_object
- mysql_fetch_row
- mysql_field_flags
- mysql_field_len
- mysql_field_name
- mysql_field_seek
- mysql_field_table
- mysql_field_type
- mysql_free_result
- mysql_get_client_info
- mysql_get_host_info
- mysql_get_proto_info
- mysql_get_server_info
- mysql_info
- mysql_insert_id
- mysql_list_dbs
- mysql_list_fields
- mysql_list_processes
- mysql_list_tables
- mysql_num_fields
- mysql_num_rows
- mysql_pconnect
- mysql_ping
- mysql_query
- mysql_real_escape_string
- mysql_result
- mysql_select_db
- mysql_set_charset
- mysql_stat
- mysql_tablename
- mysql_thread_id
- mysql_unbuffered_query
Коментарии
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...
the above (as it seems to be vaguely indicated in the mysql manual) works only with mysql 4.1+
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
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);
?>
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!
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 ;] )
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).
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);
}
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.
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.
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.
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.
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)