YZMCMS内容管理系统

首页 > 动态

YZMPHP V2.8 更新框架DB操作类

来源:原创    2024-03-12 23:41:01   编辑:管理员   浏览:1078

【重要】这篇文档内容已弃用,请查看最新版的文档https://www.yzmcms.com/dongtai/153.html


where方法的用法是YZMPHP框架查询语言的精髓,可以完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的查询操作,where方法的参数支持字符串和数组,我们建议为数组查询。


字符串条件:

$db->where('status=1 and age>18')->select();

数组条件:

// 以下为YZMPHP V2.8新增的where条件语法,旧版链接:https://www.yzmcms.com/dongtai/37.html
$where['字段']  = '字段条件';
$where['字段1']  = array('表达式','字段条件1');
$where['字段2']  = array('表达式','字段条件2');
$where['字段3']  = array('表达式','字段条件3','可选参数(函数名)');

//第三个为可选参数,第三个参数是一个函数名称,是对“字段条件”做处理,例如:
$where['cms'] = ['neq', 'yzmcms', 'trim'];
$where['id'] = ['in', ['11','22','33'], 'intval'];
$db->where($where)->select();

// 条件或查询(OR)
$db->where($where1, $where2, $where3)->select();

where方法还支持传入多个参数,以上这种多个where参数就组成了where或查询,最终执行结果为 where1 OR where2 OR where3,其中每个where既可以是数组也可以是字符串。


表达式含义(不分大小写):

eq等于(=)

neq不等于(<>)

gt大于(>)

egt大于等于(>=)

lt小于(<)

elt小于等于(<=)

like模糊查询

notlike(不在)模糊查询

[not] in(不在)in 查询

[not] between(不在)区间查询


下面我们举一些例子来说明:

$where = [];

$where['cms'] = ['eq', 'yzmcms'];
// 等同于 $where['cms'] = 'yzmcms';

$where['cms'] = ['neq', 'yzmcms'];
// 等同于 $where['cms!='] = 'yzmcms';

$where['age'] = ['gt', '18'];
// 等同于 $where['age>'] = '18';

$where['age'] = ['egt', '18'];
// 等同于 $where['age>='] = '18';

$where['age'] = ['lt', '18'];
// 等同于 $where['age<'] = '18';

$where['age'] = ['elt', '18'];
// 等同于 $where['age<='] = '18';

$where['cms'] = ['like', '%yzmcms%'];
// 等同于 $where['cms'] = '%yzmcms%';

// 新版支持的语法查询:
$where['cms'] = ['notlike', '%yzmcms%'];

$where['id'] = ['in', ['11','22','33']];
$where['id'] = ['in', ['11','22','33'], 'intval'];

$where['id'] = ['notin', ['11','22','33']];
$where['id'] = ['notin', ['11','22','33'], 'intval'];

$where['id'] = ['between', ['1','99']];
$where['id'] = ['between', ['1','99'], 'intval'];

$where['id'] = ['notbetween', ['1','99']];
$where['id'] = ['notbetween', ['1','99'], 'intval'];


// 执行查询操作并打印结果
$res = $db->where($where)->select();
P($res);

// 打印生成的SQL
$db->lastsql();



新版还更新了query方法,可根据SQL类型返回 arraybool ,并新增了第二个参数,如果SQL为查询类型,则第二个参数则可以控制是否返回二维数组,默认查询查询返回二维数组,false则返回一维数组。


// 其中[yzmcms_]表示通用表前缀,无需修改
$res = $db->query("select * from yzmcms_admin"); 
$res = $db->query("select * from yzmcms_admin", false); 

P($res);