【重要】這篇文檔內容已棄用,請查看最新版的文檔: http://www.xtremeartistry.com/dongtai/153.html
where方法的用法是YZMPHP框架查詢語言的精髓,可以完成包括普通查詢、表達式查詢、快捷查詢、區間查詢、組合查詢在內的查詢操作,where方法的參數支持字符串和數組,我們建議為數組查詢。
字符串條件:
$db->where('status=1 and age>18')->select();
數組條件:
// 以下為YZMPHP V2.8新增的where條件語法,舊版鏈接:http://www.xtremeartistry.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類型返回 array 或 bool ,并新增了第二個參數,如果SQL為查詢類型,則第二個參數則可以控制是否返回二維數組,默認查詢查詢返回二維數組,false則返回一維數組。
// 其中[yzm_]表示通用表前綴,無需修改 $res = $db->query("select * from yzm_admin"); $res = $db->query("select * from yzm_admin", false); P($res);