Using the function provided by Janez and nag worked fine for SELECTs, but with INSERT and UPDATE the non-ascii characters got converted into question marks. To fix this, I replaced SET CHARACTER SET with SET NAMES (ref: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html). So the function becomes:
<?php
if (function_exists('mysql_set_charset') === false) {
/**
* Sets the client character set.
*
* Note: This function requires MySQL 5.0.7 or later.
*
* @see http://www.php.net/mysql-set-charset
* @param string $charset A valid character set name
* @param resource $link_identifier The MySQL connection
* @return TRUE on success or FALSE on failure
*/
function mysql_set_charset($charset, $link_identifier = null)
{
if ($link_identifier == null) {
return mysql_query('SET NAMES "'.$charset.'"');
} else {
return mysql_query('SET NAMES "'.$charset.'"', $link_identifier);
}
}
}
?>
mysql_set_charset
(PHP 5 >= 5.2.3)
mysql_set_charset — Sets the client character set
Description
bool mysql_set_charset
( string $charset
[, resource $link_identifier
] )
Sets the default character set for the current connection.
Parameters
- charset
-
A valid character set name.
- 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 by chance no connection is found or established, an E_WARNING level error is generated.
Return Values
Returns TRUE on success or FALSE on failure.
Notes
Note: This function requires MySQL 5.0.7 or later.
mysql_set_charset
vljubovic AT smartnet DOT ba
04-Mar-2008 10:22
04-Mar-2008 10:22
Anonymous
10-Feb-2008 07:03
10-Feb-2008 07:03
Actually, this function is available in client libraries in MySQL 4.1.13 and newer, too. So the real version requirement is MySQL >= 5.0.7 OR, if you're using MySQL 4, then >= 4.1.13.
vk AT datarecovery D0T eu
30-Dec-2007 06:37
30-Dec-2007 06:37
A list of MySQL's 'valid character set names': http://dev.mysql.com/doc/refman/5.1/en/charset-charsets.html
nag QWE svgfr RTY org
17-Dec-2007 05:47
17-Dec-2007 05:47
Here's an improved version of Janez R.'s function:
<?php
if (function_exists('mysql_set_charset') === false) {
/**
* Sets the client character set.
*
* Note: This function requires MySQL 5.0.7 or later.
*
* @see http://www.php.net/mysql-set-charset
* @param string $charset A valid character set name
* @param resource $link_identifier The MySQL connection
* @return TRUE on success or FALSE on failure
*/
function mysql_set_charset($charset, $link_identifier = null)
{
if ($link_identifier == null) {
return mysql_query('SET CHARACTER SET "'.$charset.'"');
} else {
return mysql_query('SET CHARACTER SET "'.$charset.'"', $link_identifier);
}
}
}
?>
Janez R.
04-Sep-2007 11:23
04-Sep-2007 11:23
I assume that this is an equivalent in previous versions of php (add some parameter validation and default values though!):
<?
if (!function_exists('mysql_set_charset')) {
function mysql_set_charset($charset,$dbh)
{
return mysql_query("set names $charset",$dbh);
}
}
?>
