Here's an example of how to use this feature :
I'm using PHP 5.2.5 from http://oss.oracle.com/projects/php/
I wanted to store arabic characters as UTF8 in the database and as suggested in many of the google results, I tried using
mysql_query("SET NAMES 'utf8'");
and
mysql_query("SET CHARACTER SET utf8 ");
but it did not work.
Using the following, it worked flawlessly
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
Once this is set we need not manually encode the text into utf using utf8_encode() or other functions. The arabic ( or any UTF8 supported ) text can be passed directly to the database and it is automatically converted by PHP.
For eg.
<?php
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
$db_selected = mysql_select_db('emp_feedback', $link);
if (!$db_selected) { die ('Database access error : ' . mysql_error());}
$query = "INSERT INTO feedback ( EmpName, Message ) VALUES ('$_empName','$_message')";
mysql_query($query) or die('Error, Feedback insert into database failed');
?>
Note that here $_empName is stored in English while $_message is in Arabic.
mysql_set_charset
(PHP 5 >= 5.2.3)
mysql_set_charset — Sets the client character set
Beschreibung
bool mysql_set_charset
( string $charset
[, resource $link_identifier
] )
Sets the default character set for the current connection.
Parameter-Liste
- charset
-
A valid character set name.
- Verbindungs-Kennung
-
Die MySQL-Verbindung. Wird die Verbindungskennung nicht angegeben, wird die letzte durch mysql_connect() geöffnete Verbindung angenommen. Falls keine solche Verbindung gefunden wird, wird versucht, eine Verbindung aufzubauen, wie es beim Aufruf von mysql_connect() ohne Angabe von Argumenten der Fall wäre. Falls zufällig keine Verbindung gefunden oder aufgebaut werden kann, wird eine Warnung der Stufe E_WARNING erzeugt.
Rückgabewerte
Gibt bei Erfolg TRUE zurück, im Fehlerfall FALSE.
Anmerkungen
Hinweis: This function requires MySQL 5.0.7 or later.
mysql_set_charset
nabeelmoidu at gmail dot com
08-Jun-2008 10:11
08-Jun-2008 10:11
vljubovic AT smartnet DOT ba
04-Mar-2008 12:22
04-Mar-2008 12:22
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);
}
}
}
?>
Anonymous
10-Feb-2008 09:03
10-Feb-2008 09: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 08:37
30-Dec-2007 08: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 07:47
17-Dec-2007 07: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 01:23
04-Sep-2007 01: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);
}
}
?>
