©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PHP 5 >= 5.0.5)
mysqli::set_charset -- mysqli_set_charset — 设置默认字符编码
面向对象风格
$charset
)过程化风格
$link
, string $charset
)设置在数据库间传输字符时所用的默认字符编码。
link
仅以过程化样式:由 mysqli_connect() 或 mysqli_init() 返回的链接标识。
charset
被设为默认的字符编码名。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Note:
如果在Windows平台上使用该方法,需要4.1.11版或以上的MySQL客户端库,且MySQL版本为5.0.6以上。
Note:
这应该是首选的用于改变字符编码的方法,不建议使用 mysqli_query() 执行SQL请求的SET NAMES ...(如 SET NAMES utf8)。 详见MySQL字符集的概念
Example #1 mysqli::set_charset() example
面向对象风格
<?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "test" );
if ( mysqli_connect_errno ()) {
printf ( "Connect failed: %s\n" , mysqli_connect_error ());
exit();
}
if (! $mysqli -> set_charset ( "utf8" )) {
printf ( "Error loading character set utf8: %s\n" , $mysqli -> error );
} else {
printf ( "Current character set: %s\n" , $mysqli -> character_set_name ());
}
$mysqli -> close ();
?>
过程化风格
<?php
$link = mysqli_connect ( 'localhost' , 'my_user' , 'my_password' , 'test' );
if ( mysqli_connect_errno ()) {
printf ( "Connect failed: %s\n" , mysqli_connect_error ());
exit();
}
if (! mysqli_set_charset ( $link , "utf8" )) {
printf ( "Error loading character set utf8: %s\n" , mysqli_error ( $link ));
} else {
printf ( "Current character set: %s\n" , mysqli_character_set_name ( $link ));
}
mysqli_close ( $link );
?>
以上例程会输出:
Current character set: utf8
[#1] coder at punkass dot com [2006-04-12 11:13:41]
On windows, with "stock" PHP 5.1.2, there is no set_charset function at all. One may have to replace php_mysqli.dll, and libmysql.dll with proper versions, which provided by MySQL at http://dev.mysql.com/downloads/connector/php/