Heim  >  Artikel  >  Backend-Entwicklung  >  新手请教模糊查询怎样防止sql注入

新手请教模糊查询怎样防止sql注入

WBOY
WBOYOriginal
2016-06-23 14:03:05908Durchsuche

//创建表模型
$news_table=new news();
//创建相应的适配器
$db=$news_table->getAdapter();
//准备好sql语句
$sql=$db->quoteInto("select title,pubDate from news where title like '%$keyword_arr[0]%'");
//获取结果集
$res=$db->query($sql)->fetchAll();
我需要模糊查询,有意要带%号,而且里面还有变量名$号,数组的下标运算符[]号,但又要防止别人用%之类的这些东西来Sql注入,这句话该怎么写啊??


回复讨论(解决方案)

加一个 mysql_real_escape_string();
不过mysql_real_escape_string不转义 % 和 _ 所以可以先 str_replace(),去掉不想要的符号。

这句Sql可以帮忙写出来给我试下不
我是菜鸟啊

这句sql能查询出结果,但是在我的错误日志文件里记录了1个错误和1个警告
PHP Warning:  Missing argument 2 for Zend_Db_Adapter_Abstract::quoteInto(),
called in E:\myenv\Apache\htdocs\news\application\controllers\NewsqueryController.php on line 44 and defined in E:\myenv\Apache\htdocs\news\library\Zend\Db\Adapter\Abstract.php on line 927

PHP Notice:  Undefined variable: value in 
E:\myenv\Apache\htdocs\news\library\Zend\Db\Adapter\Abstract.php 
on line 930
不晓得哪里有问题

1、Abstract.php 927 行的 end_Db_Adapter_Abstract::quoteInto() 函数丢失了参数2,即你少传了一个参数
2、PHP Notice:  Undefined variable: value 
$value 没有定义,在 Abstract.php 930 行
 

%怎么实现这个注入的?

我也想知道,应该是要对参数进行过滤,发现要过滤的字符,就强制退出或者替换掉。

mysql_real_escape_string

mysql_escape_string

有什么分别??

$db-> quoteInto("select title,pubDate from news where title like '%$keyword_arr[0]%'");
方法 quoteInto 需要有两个参数

to #7
后者已列入过时系列

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn