php access sql转义

对sql中的单引号转义可以防止sql注入,对于其他数据库比如mysql,sql转义用 “\” 就可以了,但是在access中,使用”\”转义报错。

在access中对单引号转义使用 单引号 “‘”,比如

select * from article where title = ‘what’s it?’

如果使用 “\”转义

select * from article where title = ‘what\’s it?’,会报错:

“语法错误(操作符丢失)在查询表达式 ‘title = ‘what\’s it?”中。“

在sql中我们使用单引号转义:

select * from article where title = ‘what”s it?’

在执行sql之前把每个字段中的单引号替换即可,下面是php中的方法:

public function access_escape($str){
	return str_replace("'", "''", $str);
}

 

发表评论