PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysql_stat> <mysql_select_db
Last updated: Fri, 25 Jul 2008

view this page in

mysql_set_charset

(PHP 5 >= 5.2.3)

mysql_set_charsetSets the client character set

Descripción

bool mysql_set_charset ( string $charset [, resource $link_identifier ] )

Sets the default character set for the current connection.

Lista de parámetros

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.

Valores retornados

Devuelve TRUE si todo se llevó a cabo correctamente, FALSE en caso de fallo.

Notes

Note: This function requires MySQL 5.0.7 or later.



mysql_stat> <mysql_select_db
Last updated: Fri, 25 Jul 2008
 
add a note add a note User Contributed Notes
mysql_set_charset
nabeelmoidu at gmail dot com
08-Jun-2008 10:11
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.
vljubovic AT smartnet DOT ba
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
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
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
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
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);
  }
}
?>

mysql_stat> <mysql_select_db
Last updated: Fri, 25 Jul 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites