Heim  >  Artikel  >  Backend-Entwicklung  >  ThinkPHP3.1新特性之查询条件预处理简介_php实例

ThinkPHP3.1新特性之查询条件预处理简介_php实例

WBOY
WBOYOriginal
2016-06-07 17:18:37869Durchsuche

以往的ThinkPHP3.0版本对数组方式的查询条件会进行安全过滤(这是由于3.0强制使用了字段类型检测,所以数组方式的查询条件会强制转换为字段的设定类型),但是3.0版本并不支持字符串条件的安全过滤。而ThinkPHP3.1版本则增加了对条件字符串进行预处理的支持,让ORM的安全性更加得以保证。

一、使用where方法

Model类的where方法支持字符串条件预处理,使用方式:
$Model->where("id=%d and username='%s' and
xx='%f'",array($id,$username,$xx))->select();

或者直接使用:

$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();

如果$id变量来自用户提交或者URL地址的话,如果传入的是非数字类型,则会强制格式化为数字格式后进行查询操作。

字符串预处理格式类型支持指定数字、字符串等,具体可以参考vsprintf方法的参数说明。

二、使用query和execute方法

除了where条件外,对原生SQL查询方式也支持预处理机制,例如:

$Model->query("SELECT * FROM think_user WHERE id=%d and username='%s' and xx='%f'",array($id,$username,$xx));

模型的execute方法也和query方法一样支持预处理机制。

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