ThinkPHP3.1新特性之查询条件预处理简介
以往的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();
Copy after login
或者直接使用:
$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
Copy after login
如果$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));
Copy after login
模型的execute方法也和query方法一样支持预处理机制。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
