php连接数据库时候的字符集设置
2014-11-06 15:01
169 查看
最好的办法:设置mysql服务器的字符集,当然也可以通过mysql提供的api来设置运行时的字符集
Ideally a proper character set will be set at the server level, and doing this is described within the » Character Set Configuration section of the MySQL Server manual. Alternatively, each MySQL API offers a method to set the character set at runtime.
注意:字符集需要被设置,因为他会影响到每个请求的action,并且会影响到安全(如对mysqli_real_escape_string() ,mysql_real_escape_string(),PDO::quote())对这些函数产生影响。(用query设置的字符集,这些函数不会用到)
For example, the escaping mechanism (e.g., mysqli_real_escape_string() for mysqli, mysql_real_escape_string() for mysql, and PDO::quote() for PDO_MySQL) will adhere(坚持; 追随; ) to this setting. It is important to realize that these functions will not use the character set that is defined with a query, so for example the following will not have an effect on them:
Below are examples that demonstrate how to properly alter the character set at runtime using each API.
Example #2 Setting the character set example: mysqli
Example #3 Setting the character set example: pdo_mysql
Note: This only works as of PHP 5.3.6.
Example #4 Setting the character set example: mysql
参考:http://php.net/manual/en/mysqlinfo.concepts.charset.php#mysqlinfo.concepts.charset
Ideally a proper character set will be set at the server level, and doing this is described within the » Character Set Configuration section of the MySQL Server manual. Alternatively, each MySQL API offers a method to set the character set at runtime.
注意:字符集需要被设置,因为他会影响到每个请求的action,并且会影响到安全(如对mysqli_real_escape_string() ,mysql_real_escape_string(),PDO::quote())对这些函数产生影响。(用query设置的字符集,这些函数不会用到)
The character set and character escaping
The character set should be understood and defined, as it has an affect on every action, and includes security implications.For example, the escaping mechanism (e.g., mysqli_real_escape_string() for mysqli, mysql_real_escape_string() for mysql, and PDO::quote() for PDO_MySQL) will adhere(坚持; 追随; ) to this setting. It is important to realize that these functions will not use the character set that is defined with a query, so for example the following will not have an effect on them:
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); // Will not affect $mysqli->real_escape_string(); $mysqli->query("SET NAMES utf8"); // Will not affect $mysqli->real_escape_string(); $mysqli->query("SET CHARACTER SET utf8"); // But, this will affect $mysqli->real_escape_string(); $mysqli->set_charset('utf8'); ?>
Below are examples that demonstrate how to properly alter the character set at runtime using each API.
Example #2 Setting the character set example: mysqli
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 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()); } print_r( $mysqli->get_charset() ); ?>
Example #3 Setting the character set example: pdo_mysql
Note: This only works as of PHP 5.3.6.
<?php $pdo = new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass'); ?>
Example #4 Setting the character set example: mysql
<?php $conn = mysql_connect("localhost", "my_user", "my_pass"); $db = mysql_select_db("world"); if (!mysql_set_charset('utf8', $conn)) { echo "Error: Unable to set the character set.\n"; exit; } echo 'Your current character set is: ' . mysql_client_encoding($conn); ?>
参考:http://php.net/manual/en/mysqlinfo.concepts.charset.php#mysqlinfo.concepts.charset
相关文章推荐
- PHP 设置MySQL连接字符集的方法
- php连接mssql数据库的方法以及错误设置
- PHP 设置MySQL连接字符集的方法
- Mybatis连接mysql数据库的时候,设置字符集编码
- JDBC 和 Mybatis连接mysql数据库的时候,设置字符集编码
- PHP中PDO连接数据库中各种DNS设置方法小结
- PHP 连接数据库前设置编码 --部分
- symfony 中propel 连接数据库的字符集设置
- php连接oracle的时候字符集问题
- PHP连接数据库时编码的设置
- PHP连接数据库编码设置
- MySQL设置root密码后,PHP网页无法连接数据库。
- JDBC 和 Mybatis连接mysql数据库的时候,设置字符集编码
- 从CMD进入MySql,以及PHP连接数据库时的字符串设置
- php使用pdo连接mysql数据库如何设置发送的字符集?
- PHP 设置MySQL连接字符集的方法
- RAP(RDF API FOR PHP )设置数据库连接
- JDBC 和 Mybatis连接mysql数据库的时候,设置字符集编码
- PHP中数据库连接的设置问题
- PHP 连接 Mysql 数据库时设置编码方式(引起乱码的原因之一)