博客列表 >thinkphp安装配置/数据库的增删改查

thinkphp安装配置/数据库的增删改查

汇享科技
汇享科技原创
2022年08月30日 10:14:26374浏览

1. 安装thinkphp

使用composer进行安装composer create-project topthink/think tp

2. 数据库配置链接

找到config目录下的database.php文件进行配置

51608-cg9pxbn6b4.png

3. 数据库操作 增删改查

  • 增加数据 insert

02642-arhkn43s6ch.png

  • 增加数据 insertAll

96586-8vydecg8q46.png

  • insertGetId 返回添加数据的自增主键

32888-a8uvx4lslh8.png

  • 删除数据 delete

21422-tgupmteymtj.png

  • 软删除 useSoftDelete

51592-v5okw0stfho.png

  • 修改 update

51536-xed3e37u43e.png

  • 查询 select

53898-t6sf447rio.png

代码

  1. //1.1 增 insert 返回添加成功的条数
  2. // $data = [
  3. // 'name' => '小编',
  4. // 'email' => 'xiaobian@qq.com',
  5. // 'password' => sha1(123456),
  6. // 'reg_time' => time()
  7. // ];
  8. // $res = Db::table('user')->insert($data);
  9. // print_r($res);
  10. //1.2增 insertAll 返回添加成功的条数
  11. // $data = [
  12. // [
  13. // 'name' => '小编1',
  14. // 'email' => 'xiaobian0@qq.com',
  15. // 'password' => sha1(123456),
  16. // 'reg_time' => time()
  17. // ],
  18. // [
  19. // 'name' => '小编2',
  20. // 'email' => 'xiaobian2@qq.com',
  21. // 'password' => sha1(123456),
  22. // 'reg_time' => time()
  23. // ],
  24. // [
  25. // 'name' => '小编3',
  26. // 'email' => 'xiaobian3@qq.com',
  27. // 'password' => sha1(123456),
  28. // 'reg_time' => time()
  29. // ]
  30. // ];
  31. // $res = Db::table('user')->insertAll($data);
  32. // print_r($res);
  33. // 1.3 insertGetId 返回主键值
  34. // $data = [
  35. // 'name' => '小编4',
  36. // 'email' => 'xiaobian4@qq.com',
  37. // 'password' => sha1(123456),
  38. // 'reg_time' => time()
  39. // ];
  40. // $res = Db::table('user')->insertGetId($data);
  41. // print_r($res);
  42. //2.1 删除 delete 返回删除数据的条数 没删除则返回0
  43. // $res = Db::table('user')->where('id', '11')->delete();
  44. // echo '删除了' . $res . '条数据';
  45. //2.2 软删除 useSoftDelete 将某一个字段的值进行更改
  46. // $res = Db::table('user')->where('id', 10)->useSoftDelete('password', 2)->delete();
  47. // echo '删除了' . $res . '条数据';
  48. // 3.1 修改update 返回受到影响的条数 没有返回0
  49. // $data = [
  50. // 'password' => sha1(123456789),
  51. // ];
  52. // $res = Db::table('user')->where('id', 1)->update($data);
  53. // echo '修改了' . $res . '条数据';
  54. // 查询 select
  55. // $res = Db::table('user')->select();
  56. // printf("<pre>%s</pre>", print_r($res, true));
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议