tp数据库
连接
改配置文件
改applacation/database文件
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'tp_com',
// 用户名
'username' => 'test',
// 密码
'password' => '123456',
// 端口
'hostport' => '3306',
使用方法配置
- 使用数组
Db::connect([
// 数据库类型
'type' => 'mysql',
// 数据库连接DSN配置
'dsn' => '',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'thinkphp',
// 数据库用户名
'username' => 'root',
// 数据库密码
'password' => '',
// 数据库连接端口
'hostport' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'think_',
]);
-
使用字符串
Db::connect('mysql://root:1234@127.0.0.1:3306/thinkphp#utf8'); //数据库类型://用户名:密码@数据库地址:数据库端口/数据库名#字符集
用模型类连接
见下文检测连接成功中第三点
用查询检测是否连接成功
1.tp方法
<?php
namespace app\index\controller;
use think\Db;
class Index
{
public function data()
{
//实例化数据库类
$Db=new Db();
//查询数据
$data=$Db::table("user")->select();
dump($data);
}
}
2.使用sql语句
<?php
namespace app\index\controller;
use think\Db;
class Index
{
public function data()
{
//实例化数据库类
$Db=new Db();
//查询数据
$data=$Db::query("select * from user");
dump($data);
}
}
3.模型类定义
-
创建数据模型
命令行打开tp5所在目录,使用命令行
php think make:model app\index\model\User
直接在application/index新建文件夹model
-
连接数据库
model/user.php
use think\Model; class User extends Model { //使用数据库配置连接数据库 protected $connection = [ // 数据库类型 'type' => 'mysql', // 数据库连接DSN配置 'dsn' => '', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'thinkphp', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'think_', ]; }
-
控制器中使用
//实例化模型 $user=new user(); //查询所有数据 dump($user::all);
基本使用
- 自定义控制器
<?php
//声明命名空间
namespace app\index\controller;
//导入系统控制器类
use think\Controller;
use think\Db;
class User extends Controller
{
public function index()
{
return "user-index";
}
//增
public function add()
{
$data= Db::execute("insert into user value (111,'Mary','123456')");
//返回值是影响行数
dump($data);
}
//删
public function del()
{
// 根据主键删除
Db::table('user')->delete(1);
Db::execute("delete from user where id=1");
Db::table('user')->delete([1,2,3]);
//条件删除
Db::table('user')->where('id',1)->delete();
Db::table('user')->where('id','<',10)->delete();
}
//改
public function update()
{
Db::execute('update user set id=10 where id=2');
}
//查
public function select()
{
$data= Db::query("select * from user");
dump($data);
}
}
-
使用资源控制器
cmd:E:\phpstudy_pro\WWW\tp5>php think make:controller app\index\controller\Users
写资源路由
<?php use think\Route; Route::resource('user','index/users');
修改生成的控制器文件
controller.php
<?php namespace app\index\controller; use think\Controller; use think\Request; use think\Db; class Users extends Controller { /** * 显示资源列表 * * @return \think\Response */ public function index() { //查询资源列表 $data=Db::query("select * from users"); //dump($data); //给页面分配数据 $this->assign("data",$data); //加载页面 return view(); } /** * 显示创建资源表单页. * * @return \think\Response */ public function create() { //查询资源列表 $data=Db::query("select * from users"); //dump($data); //给页面分配数据 $this->assign("data",$data); return view(); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function save(Request $request) { //处理数据 $data=input("post."); dump($data); //执行数据库插入 //$code=Db::execute("insert into users value(null,':name',':password',':age'),$data"); $code=Db::table('users')->insert($data); if($code){ $this->success("添加成功"); }else{ $this->error("添加失败"); } } /** * 显示指定的资源 * * @param int $id * @return \think\Response */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return \think\Response */ public function edit($id) { //从数据库中查询被修改的数据 $data=Db::table('users')->where('id',$id)->find(); dump($data); //分配数据 $this->assign("data",$data); //加载页面 return view(); } /** * 保存更新的资源 * * @param \think\Request $request * @param int $id * @return \think\Response */ public function update(Request $request, $id) { //接收数据 $data=Request::instance()->except('_method'); //dump($data); $code=Db::table('users')->where('id',$id)->update([ 'name'=>$data['name'],'password'=>$data['password'],'age'=>$data['age'], ]); if($code){ $this->success("数据更新成功"); }else { $this->error("更新失败"); } } /** * 删除指定资源 * * @param int $id * @return \think\Response */ public function delete($id) { // $code=Db::table('users')->delete($id); if($code) { $this->success("删除成功"); }else{ $this->error("删除失败"); } } }
事务机制
- 出现错误时,数据库回到原始状态
MySQL事务
- 要求数据库引擎必须是InnoDB
使用
1.自动控制事务
<?php
//声明命名空间
namespace app\index\controller;
//导入系统控制器类
use think\Controller;
use think\Db;
class User extends Controller
{
public function demo()
{
//把删除的函数放在transaction代码里,使用事务机制,此时若有一条语句报错,调用函数造成的结果会回到初始状态
Db::transaction(function (){
Db::table("user")->delete(1);
Db::table("user")->delete2();
});
}
}
2.手动控制事务
<?php
//声明命名空间
namespace app\index\controller;
//导入系统控制器类
use think\Controller;
use think\Db;
use think\Exception;
class User extends Controller
{
public function demo()
{
//开启事务
Db::startTrans();
//事务
try{
//删除数据
$a=Db::table("user")->delete(1);
//若失败,抛出异常
if(!$a)
{
throw new \Exception("delete error");
}
//执行提交操作
Db::commit();
}catch (\Exception $e){
//若未成功,回滚事务
Db::rollback();
//打印错误信息
dump($e->getMessage());
}
}
}
简化版
<?php
//声明命名空间
namespace app\index\controller;
//导入系统控制器类
use think\Controller;
use think\Db;
use think\Exception;
class User extends Controller
{
public function demo()
{
//开启事务
Db::startTrans();
//事务
//删除数据
$a=Db::table("user")->delete(1);
//若失败,抛出异常
if(!$a)
{
//若未成功,回滚事务
Db::rollback();
}
else{
//执行提交操作
Db::commit();
}
}
}