mysql_set_charset()与set names addslashes 与mysql_real_escape_string

fxs_2008 2009-02-02 10:11:31
...全文
2038 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
surfchen 2009-02-02
  • 打赏
  • 举报
回复
mysqli实际在5.0.5就已经引入了这个函数,不过mysql则是在5.2.3才引入的。
Zijian_Zhang 2009-02-02
  • 打赏
  • 举报
回复
学习了。
surfchen 2009-02-02
  • 打赏
  • 举报
回复
mysql_set_charset 相当于 mysql_query("SET NAMES XXX");加上mysql.charset=xxx。

mysql.charset的意思,我来描述一下。

* 要知道mysql的客户端,在C层面,内部要初始化一个叫MySQL的结构(struct MySQL)。
* 这个MySQL结构,有一个成员,叫charset。
* 在MySQL的多字节处理里有一个叫做handler的概念。每种字符集,都有自己的handler,用来判断多字节等。
* MySQL.charset的作用,就是某些情况下,告诉mysql调用哪个handler。例如MySQL.charset="gbk"就调用gbk的那些handler
fxs_2008 2009-02-02
  • 打赏
  • 举报
回复
看了一些各位的贴子
总结一下了

第一:mysql_set_charset(PHP 5 >= 5.2.3) ,PHP大于这个版本的,用这个函数,这个函数调用的是mysql里的mysql_set_character_set(MySQL 5.0.7以上才有)
   如果你的版本均达到上述,用这个

第二:mysql_real_escape_string 优于addslashes
   mysql_real_escape_string考虑到字符集的问题
  没有看底层代码,查根据上面手册文档  mysql_real_escape_string最好与mysql_set_charset一起使用
因为都涉及字符集的问题,mysql_set_charset而是设字符集的
 
fxs_2008 2009-02-02
  • 打赏
  • 举报
回复
MySQL 5.0 Reference Manual :: 20 Connectors and APIs :: 20.9 MySQL C API :: 20.9.3 C API Function Descriptions :: 20.9.3.61 mysql_set_character_set()

20.9.3.61. mysql_set_character_set()

int mysql_set_character_set(MYSQL *mysql, const char *csname)

Description

This function is used to set the default character set for the current connection. The string csname specifies a valid character set name. The connection collation becomes the default collation of the character set. This function works like the SET NAMES statement, but also sets the value of mysql->charset, and thus affects the character set used by mysql_real_escape_string()

This function was added in MySQL 5.0.7.
fxs_2008 2009-02-02
  • 打赏
  • 举报
回复

addslashes

(PHP 4, PHP 5)

addslashes — Quote string with slashes
Description
string addslashes ( string $str )

Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).




mysql_real_escape_string

(PHP 4 >= 4.3.0, PHP 5)

mysql_real_escape_string — Escapes special characters in a string for use in a SQL statement
Description
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )

Escapes special characters in the unescaped_string , taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used.

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.

ysql_real_escape_string — 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集
说明
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )

本函数将 unescaped_string 中的特殊字符转义,并计及连接的当前字符集,因此可以安全用于 mysql_query()。


fxs_2008 2009-02-02
  • 打赏
  • 举报
回复
“SET NAMES utf8;”,这个用法是错误的,在某些编码下存在漏洞。在php5.0.5以后,都应该用mysqli_set_charset(类似的是mysql_set_charset)。
http://cn.php.net/manual/en/mysqli.set-charset.php

至于为什么mysql_query("set names xxx")存在漏洞,说起来就比较复杂了,一句话就是:某些编码里,\会作为字符的一部分,导致串解析异常。mysqli_set_charset的作用是除了client,connection,result之外,还设置了mysql的某个内部结构的charset。

不要再使用set names了,也不要再叫别人去使用它了

红字的部分具体指的设的是哪个?
fxs_2008 2009-02-02
  • 打赏
  • 举报
回复

mysql_set_charset

(PHP 5 >= 5.2.3)

mysql_set_charset — Sets the client character set
Description
bool mysql_set_charset ( string $charset [, resource $link_identifier ] )

Sets the default character set for the current connection.
Parameters

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.

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧