【重要】這篇文檔內(nèi)容已棄用,請(qǐng)查看最新版的文檔: https://www.yzmcms.com/dongtai/153.html
where方法的用法是YZMPHP框架查詢語(yǔ)言的精髓,可以完成包括普通查詢、表達(dá)式查詢、快捷查詢、區(qū)間查詢、組合查詢?cè)趦?nèi)的查詢操作,where方法的參數(shù)支持字符串和數(shù)組,我們建議為數(shù)組查詢。
$db->where('status=1 and age>18')->select();
// 以下為YZMPHP V2.8新增的where條件語(yǔ)法,舊版鏈接:https://www.yzmcms.com/dongtai/37.html $where['字段'] = '字段條件'; $where['字段1'] = array('表達(dá)式','字段條件1'); $where['字段2'] = array('表達(dá)式','字段條件2'); $where['字段3'] = array('表達(dá)式','字段條件3','可選參數(shù)(函數(shù)名)'); //第三個(gè)為可選參數(shù),第三個(gè)參數(shù)是一個(gè)函數(shù)名稱,是對(duì)“字段條件”做處理,例如: $where['cms'] = ['neq', 'yzmcms', 'trim']; $where['id'] = ['in', ['11','22','33'], 'intval']; $db->where($where)->select(); // 條件或查詢(OR) $db->where($where1, $where2, $where3)->select();
where方法 還支持 傳入多個(gè) 參數(shù),以上這種多個(gè)where參數(shù)就組成了where或查詢,最終執(zhí)行結(jié)果為 where1 OR where2 OR where3,其中每個(gè)where既可以是數(shù)組也可以是字符串。
eq等于(=)
neq不等于(<>)
gt大于(>)
egt大于等于(>=)
lt小于(<)
elt小于等于(<=)
like模糊查詢
notlike(不在)模糊查詢
[not] in(不在)in 查詢
[not] between(不在)區(qū)間查詢
下面我們舉一些例子來(lái)說(shuō)明:
$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%'; // 新支持的語(yǔ)法查詢: $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']; // 執(zhí)行查詢操作并打印結(jié)果 $res = $db->where($where)->select(); P($res); // 打印生成的SQL $db->lastsql();
新版還更新了query方法,可根據(jù)SQL類型返回 array 或 bool ,并新增了第二個(gè)參數(shù),如果SQL為查詢類型,則第二個(gè)參數(shù)則可以控制是否返回二維數(shù)組,默認(rèn)查詢查詢返回二維數(shù)組,false則返回一維數(shù)組。
// 其中[yzmcms_]表示通用表前綴,無(wú)需修改 $res = $db->query("select * from yzmcms_admin"); $res = $db->query("select * from yzmcms_admin", false); P($res);