数据集处理与thinkphp请求响应

  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\Facade\Db;
  5. use think\facade\Request;
  6. class Index extends BaseController
  7. {
  8. public function index()
  9. {
  10. return '890';
  11. }
  12. public function test($name = 'ThinkPHP6')
  13. {
  14. return 'hello,' . $name;
  15. }
  16. public function testsql($name = 'ThinkPHP6')
  17. {
  18. //原生sql写法 query查询
  19. $query = Db::query("select * from `shop_goods` where status =1");
  20. //print_r($query);
  21. foreach($query as $item){
  22. //print_r($item['title'].'<br>');
  23. }
  24. //原生sql写法 execute执行insert和update
  25. // INSERT
  26. // Db::execute("INSERT INTO
  27. // `testsql`.`shop_goods`(`cat`, `title`, `price`, `discount`, `stock`, `status`, `add_time`)
  28. // VALUES (2, '云朵般轻盈的仙女裙 高级钉珠收腰长裙 气质无袖连衣裙', 279.99, 0, 1100, 1, 1576080000)");
  29. // UPDATE
  30. // Db::execute("UPDATE `testsql`.`shop_goods` SET `status` = 4 WHERE `id` = 29");
  31. //TP写法
  32. // $tpquery =Db::table('shop_goods')->find(5);
  33. // $tpquery =Db::table('shop_goods')->select();
  34. // $tpquery =Db::table('shop_goods')->where('id',2)->value('title');
  35. // $tpquery =Db::table('shop_goods')->column('title');
  36. // $tpquery =Db::table('shop_goods')->column('title','id');
  37. // print_r($tpquery);
  38. $data = [
  39. 'title'=>'555测试title插入数据',
  40. 'cat'=>'2',
  41. 'discount'=>'0',
  42. 'price'=>'100',
  43. 'add_time'=>'1576080000',
  44. ];
  45. $deletedata = [
  46. 'delete'=>1,
  47. 'update_time'=>'2576080000',
  48. ];
  49. // $tpinsert =Db::table('shop_goods')->where('id',5)->save($data);
  50. // $tpinsert =Db::table('shop_goods')->where('id',5)->update($data);
  51. // $tpinsert =Db::table('shop_goods')->insert($data);
  52. // $tpinsert =Db::table('shop_goods')->where('id',20)->delete();
  53. // $tpinsert =Db::table('shop_goods')->where('id',10)->update($deletedata);
  54. // print_r($tpinsert);
  55. //软删除
  56. // $delete = Db::table('shop_goods')->useSoftDelete('status',3)->delete();
  57. //inc dec order
  58. $decinc = Db::table('shop_goods')->where('id',7)->inc('stock')->update();
  59. $decdec = Db::table('shop_goods')->where('id',7)->dec('stock')->update();
  60. // $dec = Db::table('shop_goods')->where('id',7)->dec('stock',5)->update();
  61. $testsql = Db::table('shop_goods')->select()->order('id','desc');
  62. //print_r($testsql->toArray());
  63. $select = Db::table('shop_goods')
  64. ->field('title,price,id')
  65. ->page(2,5)
  66. ->select();
  67. //print_r($select->toArray());
  68. //聚合查询
  69. $count = Db::table('shop_goods')->count();
  70. $avg = Db::table('shop_goods')->avg('stock');
  71. $max = Db::table('shop_goods')->max('stock');
  72. //echo $max;
  73. //事务成功
  74. // Db::transaction(function(){
  75. // 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)");
  76. // Db::execute("UPDATE `testsql`.`shop_goods` SET `title` = '事务修改成功' WHERE `id` = 31");
  77. // });
  78. //事务失败回滚-InnoDB引擎支持事务处理,MyISAM不支持事务处理,方法操作数据库事务,当闭包中的代码发生异常会自动回滚
  79. //InnoDB引擎比MyISAM慢但是安全一般用在用户、支付、增加扣减相关的表
  80. // Db::transaction(function(){
  81. // 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)");
  82. // Db::execute("UPDATE `testsql`.`shop_good` SET `title` = '事务修改失败' WHERE `id` = 31");
  83. // });
  84. //数据集处理isEmpty toArray
  85. // $tpquery =Db::table('shop_goods')->where('status',8)->select();
  86. $tpquery =Db::table('shop_goods')->select();
  87. if($tpquery->isEmpty()){
  88. echo "数据为空";
  89. }else{
  90. print_r($tpquery->toArray());
  91. print_r($tpquery->column('title'));
  92. }
  93. }
  94. public function testRequest(){
  95. //http://tpmall.com/index.php/Index/testRequest?a=1
  96. // print_r($_GET);
  97. // print_r(Request::get());
  98. // echo '<br>';
  99. // print_r(Request::param('id'));
  100. // print_r(Request::post());
  101. $params = Request::post('price/d',0);
  102. $tpquery = Db::table('shop_goods')->where('price','<',$params)->select();
  103. print_r($tpquery);
  104. }
  105. }

更多相关文章

  1. Android多文件断点续传(二)——实现数据库储存下载信息
  2. 20172323 2017-2018-2《程序设计与数据结构》第十一周学习总结
  3. Android核心功能
  4. Android(安卓)轻量级存储方案的前世今生
  5. Android(安卓)SQLite
  6. Android(安卓)Volley完全解析(三),定制自己的Request
  7. Android(安卓)通过代码实现控制数据网络的开关(仅适用于5.0以上)
  8. Android之SQLite开发(2)—SQLiteOpenHelper类
  9. Android中使用Gson解析JSON数据

随机推荐

  1. 安装python2.6.6到ubuntu12.04
  2. Java程序不像python程序那样工作,我不知道
  3. python中list的拷贝与numpy的array的拷贝
  4. Python之sorted内置函数
  5. Python学习笔记(基础篇)_014_GUI模块 eas
  6. python函数介绍及使用
  7. python 操作excel 读写同一个文件
  8. Emacs中的Python 2和3都是如此
  9. Python测试函数和类 笨方法学习Python
  10. python将回车作为输入内容