PHP mysqli_set_charset()Functions
Set the default client character set:
<?php
// 假定数据库用户名:root,密码:123456,数据库:EXAMPLE
$con=mysqli_connect("localhost","root","123456","EXAMPLE");
if (mysqli_connect_errno($con))
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
// 修改数据库连接字符集为 utf8
mysqli_set_charset($con,"utf8");
mysqli_close($con);
?>
This operation is very useful for Chinese characters; many cases of garbled database queries are due to character set issues.
Definition and Usage
The mysqli_set_charset() function specifies the default character set to use when sending data to and from the database server.
Note:To use this function on the Windows platform, you need MySQL client library 4.1.11 or above (MySQL 5.0 requires 5.0.6 or above).
Syntax
mysqli_set_charset(connection,charset);
| Parameters | Description |
|---|---|
| connection | Required. Specifies the MySQL connection to use. |
| charset | Required. Specifies the default character set. |
Technical Details
| Return value: | Returns TRUE on success, or FALSE on failure. |
|---|---|
| PHP Version: | 5.0.5+ |
PHP MySQLi Reference Manual Other Extensions