TP数据集处理\请求响应\模型(二)
16lz
2022-05-13
数据集处理与thinkphp请求响应
<?php
namespace app\controller;
use app\BaseController;
use think\Facade\Db;
use think\facade\Request;
class Index extends BaseController
{
public function index()
{
return '890';
}
public function test($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
public function testsql($name = 'ThinkPHP6')
{
//原生sql写法 query查询
$query = Db::query("select * from `shop_goods` where status =1");
//print_r($query);
foreach($query as $item){
//print_r($item['title'].'<br>');
}
//原生sql写法 execute执行insert和update
// INSERT
// Db::execute("INSERT INTO
// `testsql`.`shop_goods`(`cat`, `title`, `price`, `discount`, `stock`, `status`, `add_time`)
// VALUES (2, '云朵般轻盈的仙女裙 高级钉珠收腰长裙 气质无袖连衣裙', 279.99, 0, 1100, 1, 1576080000)");
// UPDATE
// Db::execute("UPDATE `testsql`.`shop_goods` SET `status` = 4 WHERE `id` = 29");
//TP写法
// $tpquery =Db::table('shop_goods')->find(5);
// $tpquery =Db::table('shop_goods')->select();
// $tpquery =Db::table('shop_goods')->where('id',2)->value('title');
// $tpquery =Db::table('shop_goods')->column('title');
// $tpquery =Db::table('shop_goods')->column('title','id');
// print_r($tpquery);
$data = [
'title'=>'555测试title插入数据',
'cat'=>'2',
'discount'=>'0',
'price'=>'100',
'add_time'=>'1576080000',
];
$deletedata = [
'delete'=>1,
'update_time'=>'2576080000',
];
// $tpinsert =Db::table('shop_goods')->where('id',5)->save($data);
// $tpinsert =Db::table('shop_goods')->where('id',5)->update($data);
// $tpinsert =Db::table('shop_goods')->insert($data);
// $tpinsert =Db::table('shop_goods')->where('id',20)->delete();
// $tpinsert =Db::table('shop_goods')->where('id',10)->update($deletedata);
// print_r($tpinsert);
//软删除
// $delete = Db::table('shop_goods')->useSoftDelete('status',3)->delete();
//inc dec order
$decinc = Db::table('shop_goods')->where('id',7)->inc('stock')->update();
$decdec = Db::table('shop_goods')->where('id',7)->dec('stock')->update();
// $dec = Db::table('shop_goods')->where('id',7)->dec('stock',5)->update();
$testsql = Db::table('shop_goods')->select()->order('id','desc');
//print_r($testsql->toArray());
$select = Db::table('shop_goods')
->field('title,price,id')
->page(2,5)
->select();
//print_r($select->toArray());
//聚合查询
$count = Db::table('shop_goods')->count();
$avg = Db::table('shop_goods')->avg('stock');
$max = Db::table('shop_goods')->max('stock');
//echo $max;
//事务成功
// Db::transaction(function(){
// Db::execute("INSERT INTO `testsql`.`shop_goods`(`cat`, `title`, `price`, `discount`, `stock`, `status`, `add_time`, `delete`, `update_time`) VALUES (2, '事务测试title插入数据', 100.00, 0, 1, 1, 1576080000, 0, 0)");
// Db::execute("UPDATE `testsql`.`shop_goods` SET `title` = '事务修改成功' WHERE `id` = 31");
// });
//事务失败回滚-InnoDB引擎支持事务处理,MyISAM不支持事务处理,方法操作数据库事务,当闭包中的代码发生异常会自动回滚
//InnoDB引擎比MyISAM慢但是安全一般用在用户、支付、增加扣减相关的表
// Db::transaction(function(){
// Db::execute("INSERT INTO `testsql`.`shop_goods`(`cat`, `title`, `price`, `discount`, `stock`, `status`, `add_time`, `delete`, `update_time`) VALUES (2, '事务失败测试title插入数据', 100.00, 0, 1, 1, 1576080000, 0, 0)");
// Db::execute("UPDATE `testsql`.`shop_good` SET `title` = '事务修改失败' WHERE `id` = 31");
// });
//数据集处理isEmpty toArray
// $tpquery =Db::table('shop_goods')->where('status',8)->select();
$tpquery =Db::table('shop_goods')->select();
if($tpquery->isEmpty()){
echo "数据为空";
}else{
print_r($tpquery->toArray());
print_r($tpquery->column('title'));
}
}
public function testRequest(){
//http://tpmall.com/index.php/Index/testRequest?a=1
// print_r($_GET);
// print_r(Request::get());
// echo '<br>';
// print_r(Request::param('id'));
// print_r(Request::post());
$params = Request::post('price/d',0);
$tpquery = Db::table('shop_goods')->where('price','<',$params)->select();
print_r($tpquery);
}
}
更多相关文章
- Android多文件断点续传(二)——实现数据库储存下载信息
- 20172323 2017-2018-2《程序设计与数据结构》第十一周学习总结
- Android核心功能
- Android(安卓)轻量级存储方案的前世今生
- Android(安卓)SQLite
- Android(安卓)Volley完全解析(三),定制自己的Request
- Android(安卓)通过代码实现控制数据网络的开关(仅适用于5.0以上)
- Android之SQLite开发(2)—SQLiteOpenHelper类
- Android中使用Gson解析JSON数据