Blogger Information
Blog 33
fans 3
comment 0
visits 22763
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkPHP学习-数据库增删改查20180524
MrZ的博客
Original
714 people have browsed it

一、学习知识点

1,了解框架对数据库操作的几个类文件。

2,正确配置数据库的连接串。

3,应用正常引用数据库的命名空间即可使用。

4,了解和掌握框架对数据库的原生操作方法和链式调用。

5,查询

查询单条使用:find()方法,查询多条使用select()方法

6,新增

单条:使用insert(),多条使用insertALL()

7,修改更新

使用update()方法

8,删除

使用delete()方法

注意:删除和更新必须前置查询条件,使用where()方法

二、代码部分

实例

<?php 
namespace app\admin\controller;
use think\Db;
/**
 * 
 */
class Test
{
	
	public function sql()
	{
	   // $sql="select * from baoming where id >:id";

	   // $res=Db::query($sql,['id'=>55]);

	   // dump($res);

        $sql="insert baoming set name=:name,email=:email";


        $res=Db::execute($sql,['name'=>"小花1","email"=>"123@qq.com"]);

        if ($res) {
        	return "插入成功";
        }

	}

   public function select()
   {
   	 $data=["id"=>"45"];
     $res=Db::table("baoming")->where("id",">","40")->order("name")->find();
   dump($res);

   }


      public function insert()
   {
   	 $date=["name"=>"xiaohahaha","email"=>"555@qq.com"];
     $res=Db::table("baoming")->data($date)->insert();
   dump($res);

   }

         public function update()
   {
   	 // $date=["name"=>"xiaohahaha","ema0il"=>"555@qq.com"];
     // $res=Db::table("baoming")->data($date)->where("id","45")->update();
$date=["c_id"=>Db::raw("c_id+888")];
$res=Db::table("baoming")->where("id",">","46")->data($date)->update();
   }


          public function del()
   {
   	 // $date=["name"=>"xiaohahaha","email"=>"555@qq.com"];
     $res=Db::table("baoming")->where("id","54")->delete();
   dump($res);

   }



}



 ?>

运行实例 »

点击 "运行实例" 按钮查看在线实例



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