搭建tp环境,了解配置文件,实战操作数据库

原声操作数据库

  • 原声mysql (查询所有数据)
    1. $res=Db::query("SELECT * FROM `shop_goods` " );
    2. if(!empty($res)){
    3. foreach($res as $key){
    4. echo $key['title'];
    5. echo '<br>';
    6. }
    7. }
    8. printf('<pre>%s</pre>',print_r($res,true));
  • 原声mysql 添加 和修改execute 方法用于执行 MySql 新增和修改操作

    • 查询数据库里有多少条数句
      $res = Db::execute("SELECT * FROM `shop_goods` ");// echo $res;
    • 添加
      1. $res = Db::execute("INSERT INTO `lx`.`shop_goods` (`id`, `cat`, `title`, `price`, `discount`, `stock`, `status`, `add_time`) VALUES (null, 1, '内衣学姐风', 300.00, 0, 100, 1, 1576080000); ");
      2. // echo $res
    • 修改
      1. $res = Db::execute("UPDATE `lx`.`shop_goods` SET `add_time` = 1380013801 WHERE `id` = 24; ");
      2. // echo $res

      tp

    • select 查询全部
      1. $res = Db::table('shop_goods')->select();
      2. if(!empty($res)){
      3. foreach($res as $key){
      4. echo $key['title'];
      5. echo '<br>';
      6. }
      7. }
      8. printf('<pre>%s</pre>',print_r($res,true));
    • find 查询单条数据 要增加条件
      $res = Db::table('shop_goods')->find('1');printf('<pre>%s</pre>',print_r($res,true));
    • value 查询单个数据 默认查询第一个
      $res = Db::table('shop_goods')->value('title');//printf('<pre>%s</pre>',print_r($res,true));
    • where 条件
      $res = Db::table('shop_goods')->where('id',20)->value('title');// printf('<pre>%s</pre>',print_r($res,true));
    • column 查询一列
      $res = Db::table('shop_goods')->column('id');// printf('<pre>%s</pre>',print_r($res,true));id 为索引进行查询// $ress = Db::table('shop_goods')->column('title','id');// printf('<pre>%s</pre>',print_r($ress,tr
    • insert 插入

      1. arr =[
      2. 'id'=>'',
      3. 'cat'=>'2',
      4. 'title'=>'苹果X',
      5. 'price'=>'1688',
      6. 'discount'=>'200',
      7. 'stock'=>'300',
      8. 'status'=>'1',
      9. 'add_time'=>'123',
      10. ];
      11. $res = Db::table('shop_goods')->insert($arr);
      12. printf('<pre>%s</pre>',print_r($res,true));
    • insertGetId 查询插入的ID

      1. $arr =[
      2. 'id'=>'',
      3. 'cat'=>'2',
      4. 'title'=>'苹果X',
      5. 'price'=>'1688',
      6. 'discount'=>'200',
      7. 'stock'=>'300',
      8. 'status'=>'1',
      9. 'add_time'=>'123',
      10. ];
      11. $res = Db::table('shop_goods')->insertGetId($arr);
      12. printf('<pre>%s</pre>',print_r($res,true));
    • insertAll 插入多条数据

      1. //insertAll 插入多条数据
      2. $arr =[
      3. [
      4. 'id'=>'',
      5. 'cat'=>'2',
      6. 'title'=>'苹果X',
      7. 'price'=>'1688',
      8. 'discount'=>'200',
      9. 'stock'=>'300',
      10. 'status'=>'1',
      11. 'add_time'=>'123',
      12. ],
      13. [
      14. 'id'=>'',
      15. 'cat'=>'2',
      16. 'title'=>'苹果12',
      17. 'price'=>'1588',
      18. 'discount'=>'200',
      19. 'stock'=>'300',
      20. 'status'=>'1',
      21. 'add_time'=>'123',
      22. ],
      23. [
      24. 'id'=>'',
      25. 'cat'=>'2',
      26. 'title'=>'苹果13',
      27. 'price'=>'168',
      28. 'discount'=>'200',
      29. 'stock'=>'300',
      30. 'status'=>'1',
      31. 'add_time'=>'123',
      32. ],
      33. ];
      34. $res = Db::table('shop_goods')->insertAll($arr);
      35. printf('<pre>%s</pre>',print_r($res,true));
    • update 修改
      $res = Db::table('shop_goods') ->where('id',42)->update(['title'=>'隔壁老王手机']);
    • where 条件查询

      1. //默认的是等于
      2. //$res = Db::table('shop_goods') ->where('id',40)->select();
      3. //大于 >
      4. //$res = Db::table('shop_goods') ->where('id','>',10)->select();
      5. //小于<
      6. //$res = Db::table('shop_goods') ->where('id','<',18)->select();
      7. //大于等于
      8. //$res = Db::table('shop_goods') ->where('id','>=',10)->select();
      9. //小于等于
      10. //$res = Db::table('shop_goods') ->where('id','<=',10)->select();
      11. //printf('<pre>%s</pre>',print_r($res,true));
    • field 字段返回值

      1. res = Db::table('shop_goods') ->field(['title','id'])->select();
      2. printf('<pre>%s</pre>',print_r($res,true));
      3. $arr=['ouyangke'=>'欧阳克'];
      4. // printf('<pre>%s</pre>',print_r($arr,true));
      5. // echo $arr['ouyangke'];
      6. // order 排序
      7. // $res = Db::table('shop_goods') ->order('id DESC')->select();
      8. // printf('<pre>%s</pre>',print_r($res,true));
    • limit 查询几条
      1. $res = Db::table('shop_goods') ->field(['title','id'])->limit(0,5)->select();
      2. printf('<pre>%s</pre>',print_r($res,true));
      3. //page 翻页 更方便
      4. $res = Db::table('shop_goods')->order('id DESC')->page(3,3)->select(); printf('<pre>%s</pre>',print_r($res,true));

更多相关文章

  1. CSS:媒体查询-复习
  2. Android中数据存储的5中方法
  3. 学习笔记(六) 网络编程与数据处理
  4. Android(安卓)之 ContentProvider 共享数据库-通讯录
  5. Android(安卓)apk间通过设置sharedUserId共享资源
  6. Android(安卓)- SQLite in Android
  7. Android访问WCF(下篇)-客户端开发
  8. Android(安卓)系统数据库编程学习日志
  9. Android的string-array数据源简单使用

随机推荐

  1. android 图片自动切换
  2. Android(安卓)TextView中显示图片的4种方
  3. Handler、HandlerThread理解
  4. android 入门xml布局文件--转
  5. Android(安卓)View如何获取焦点
  6. shape画listview分割线
  7. Fragment、Activity比较——Android碎片
  8. Android(安卓)中ListView setOnItemClick
  9. android之知识点小结一
  10. Android布局属性详解