数据库中遇到的操作
- 查询字段是json的某个值
//获取数据库中某个字段是json中得某个值,进行查询,goods是表中字段,brand_id是json中要查詢的字段。//数据类型一定要对应要不然查询不出来。$map['json_extract(goods, "$.brand_id")'] = (int)$params['brand_id'];
- 查询结果进行遍历操作:
db('product_plant')->where($map)->order('id', 'desc')->paginate($limit)->each(function ($item, $key) use ($store_id) {//代码逻辑}
- 多级下拉
if (isset($params['cat']) && !empty($params['cat'])) {if (count($params['cat']) == 1) {$shop_cat_array = model('store_category')->where(['pid'=>$params['cat'][0], 'store_id'=>$this->auth->id])->column('id');$cat = model('store_category')->where('pid', 'in', $shop_cat_array)->where('store_id', $this->auth->id)->column('id');$map['shop_cat'] = ['in', $cat];}else if (count($params['cat']) == 2) {$cat = model('store_category')->where(['pid'=>$params['cat'][1], 'store_id'=>$this->auth->id])->column('id');$map['shop_cat'] = ['in', $cat];}else {$map['shop_cat'] = $params['cat'][2];}}
$field = 'p.id';
$sql = db('product')->alias('p')->field($field)->where($where)->union(function ($query) use ($where,$field) {$query->name('store_product')->alias('p')->field($field)->where($where);
}, true)->buildSql();
$model = Db::table($sql . ' as a');
$productIds = $model->column('id');
$map['product_id'] = ['in', $productIds];$this->request->domain()$bill_money = db('store_bill')->where(array_merge(['comein'=>1],$w))->sum('price')-db('store_bill')->where(array_merge(['comein'=>0],$w))->sum('price');
-
查看数据库中json字段不符合json结构的:
SELECT *
FROM hzqc_product_plant
WHERE JSON_VALID(goods) = 0; -
不符合json结构不能使用json_container()
-
mysql相关函数:json_valid()
-
php密码设置
$params[‘salt’] = Random::alnum();
p a r a m s [ ′ p a s s w o r d ′ ] = m d 5 ( m d 5 ( params['password'] = md5(md5( params[′password′]=md5(md5(params[‘password’]) . $params[‘salt’]);
SELECT * FROM hzqc_product_plant
WHERE store_id
= 91000003 AND json_extract(goods, " . b r a n d i d " ) = 6074 S E L E C T ∗ F R O M ‘ h z q c p r o d u c t p l a n t ‘ W H E R E ‘ s t o r e i d ‘ = 91000003 A N D g o o d s − > ′ .brand_id") = 6074 SELECT * FROM `hzqc_product_plant` WHERE `store_id` = 91000003 AND goods->' .brandid")=6074SELECT∗FROM‘hzqcproductplant‘WHERE‘storeid‘=91000003ANDgoods−>′.“brand_id”’ = 6074;
- 常用的一些东西可以放到commom.php中,在使用的时候直接调用
if (!function_exists('cdnurl')) {/*** 获取上传资源的CDN的地址* @param string $url 资源相对地址* @return string*/function cdnurl($url) {return preg_match("/^https?:\/\/(.*)/i", $url) ? $url : think\Config::get('upload.cdnurl') . $url;}
}//常用常量参数也可以放到common中
if (!function_exists('mall_payment_type')) {function mall_payment_type($code = '9') {$result = ['wechat'=>'微信支付', 'alipay'=>'支付宝', 'credit'=>'信用支付', 'transfer'=>'对公转账', 'deposit'=>'预付款', 'zero'=>'零元领取礼包', 'mixed'=>'混合支付'];return ($code == '9') ? $result : $result[$code]??"";}
}//随机获取一个IP
if (!function_exists('rand_id')) {function rand_id(){$data = array(119.120.'.'.rand(1,255).'.'.rand(1,255),124.174.'.'.rand(1,255).'.'.rand(1,255),116.249.'.'.rand(1,255).'.'.rand(1,255),118.125.'.'.rand(1,255).'.'.rand(1,255),42.175.'.'.rand(1,255).'.'.rand(1,255),124.162.'.'.rand(1,255).'.'.rand(1,255),211.167.'.'.rand(1,255).'.'.rand(1,255),58.206.'.'.rand(1,255).'.'.rand(1,255),117.24.'.'.rand(1,255).'.'.rand(1,255),203.93.'.'.rand(1,255).'.'.rand(1,255),);//随机获取一个IP地址$ip = $data[array_rand($data)];return $ip;}
}