Blogger Information
Blog 22
fans 0
comment 0
visits 11570
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实战-TP数据库操作(一)
没耐心的渔翁
Original
458 people have browsed it

搭建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));
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post