通过SQL语句直接对数据库进行操作

namespace app\controller;
use think\Controller;
//导入Db 类库
use think\Db;
class User extends Controller
{

public function demo()
{
$result = Db::table('user')->select();
dump($result);
}

public function demo1()
{

//查询数据库
$sql = "select name from user";
$result = Db::query($sql);
dump($result);

//更新数据库
$sql = "update user set password = '123456' where userid=:id";
$result = Db::execute($sql,['id'=>1]);
dump($result);

//插入数据库
$sql = "insert into user (name,password) values (:name,:password)";
$result = Db::execute($sql,['name'=>'root','password'=>'123456']);
dump($result);

//删除记录
$sql = "delete from user where userid=:id";
$result = Db::execute($sql,['id'=>2]);
dump($result);
}
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注