您好,欢迎访问一九零五行业门户网

FleaPHP框架数据库查询条件($conditions)写法总结_php实例

本文实例讲述了fleaphp框架数据库查询条件($conditions)写法。分享给大家供大家参考,具体如下:
在fleaphp中,凡是用到数据库查询的函数,都需要查询条件参数$conditions,现讲述用法如下:
举例:
// $conditions 保存查询条件$conditions = 'level_ix > 1';// $tableorders 是一个订单数据表的表数据入口对象$order = $tableorders->find($conditions, 'created desc', 'id, title, body');$conditions = array('username' => 'dualface');// $tableusers 是一个用户信息数据表的表数据入口对象$user = $tableusers->find($conditions);
$conditions 参数可以是整数、字符串和数组三种类型:
1.如果 $conditions 参数是一个整数,则假定该整数为主键字段值。
// 查询主键字段值为1的记录$user = $tableusers->find(1);// 如果主键字段名为id,则生成的where字句为where `id` = 1
2.如果 $conditions 参数是一个字符串,则该字符串将直接作为查询条件,这种方式可以支持最灵活的查询条件。 例如:
$conditions = 'id find($conditions);//生成的where字句为where id '1', );$user = $tableusers->find($conditions);//生成的where字句为where `id` = 1
3.2.如果 $conditions 参数是一个数组,但其中的元素没有键名, 则假定键值为自定义查询条件,例如:
$conditions = array('id = 1');// 生成的where字句为where `id` = 1$user = $tableusers->find($conditions);
3.3.$conditions 为数组时,可以混用字符串和键值对两种风格:
$conditions = array( 'id 'male',);$user = $tableusers->find($conditions);// 生成的where字句为id array(1,2,3),)$user = $tableusers->find($conditions);// 生成的where子句为where `id` in (1, 2, 3)
那么如果不是主键的话怎么写了呢? 也很简单,提供键值对即可。例如:
$condition = array( 'in()' => array( 'username' => array('username1','username2') ) )$user = $tableusers->find($conditions);// 生成的where子句为where `username` in ('username1', 'username2')
4.find()函数中其它参数的含义和用法如下:
4.1.$sort 参数指定查询时的排序方式,类型只能为字符串
例如 'created asc' 表示按照created字段进行从小到大的排序。
4.2.$fields 参数指定查询结果中要包含哪些字段,类型可以为字符串或数组
当数据表的字段很多时,通过指定 $fields 参数可以避免查询不需要的字段,从而提高性能。
$fields 参数即可是以,逗号分隔的字段名,也可以是包含多个字段名的数组,例如:
$fields = array('title', 'created');//也可以写成下面的字符串形式,两种写法作用相同,区别在于自动生成的字段名两边将会添加上`符号,以防止出现字段名与sql关键字冲突的情况出现。建议手写时也加上`字符$fields = 'title, created';$user = $tableusers->find('id < 10',null,$fields);
推荐使用数组,这样表数据入口处理起来更快一些。
希望本文所述对大家基于fleaphp框架的php程序设计有所帮助。
其它类似信息

推荐信息