一、数据库链接方式
注:用户访问URL对应的是一个控制器,控制器与模型(数据库)交互,然后把处理完的数据前给视图,视图负责展现数据。
<?php
namespace app\index\controller;
use think\Db;
/*
* 数据库链接方式
* 1.全局配置:config/database.php
* 2.动态配置
* 3.DES配置
*/
class Demo4
{
//全局配置
public function conn1($id = 1)
{
return Db::table('student')
->where('id', $id)
->value('name');
}
//动态配置
public function conn2($id = 1)
{
return Db::connect([
'type' => 'mysql',
'hostname' => '127.0.0.1',
'database' => 'demo',
'username' => 'root',
'password' => 'root'
])
->table('student')
->where('id', $id)
->value('name');
}
//DSC配置
public function conn3($id = 1)
{
$dsc = 'mysql://root:root@127.0.0.1:3306/demo#utf8';
return Db::connect($dsc)
->table('student')
->where('id', $id)
->value('name');
}
}
二、数据库操作
<?php
namespace app\index\controller;
use think\Db;
use think\db\Where;
class Demo5
{
//1.单条查询
public function find($id = 1)
{
$res = Db::table('student')
// ->field('id,name,email')
->field(['id' => '编号', 'name' => '姓名', 'email' => '邮箱'])
// ->where('id','=',$id)//默认查询条件是=号,可以省略,如果查询条件是主键,where语句也可以省略
->find(2);//省略where后,在Find()中放入查询主键参数
var_dump(is_null($res) ? '没有找到' : $res);
}
//2.多条查询
public function select()
{
$res = Db::table('student')
->field('id,name,course,grade')
->where([
['course', '=', 'php'],
['grade', '>', 80]
])->select();
if (empty($res)) {
return '没有满足条件的记录';
} else {
foreach ($res as $row) {
dump($row);
}
}
}
//3.单条插入
public function insert()
{
//insert()成功返回新增的数量,失败返回false
$data = [
'name' => '测试6',
'email' => 'cs6@qq.com',
'course' => 'php',
'grade' => '85',
'create_time' => time(),
'update_time' => time(),
];
// $res = Db::table('student')->insert($data);
//insert()中加,true参数,生成的SQL语句是以 replace方法插入数据,效率更高(其实也要具体分析)
// $res = Db::table('student')->insert($data,true);
//data($data)方法,会过滤参数,更安全
// $res = Db::table('student')->data($data)->insert();
//插入的同时返回新增主键
$res = Db::table('student')->insertGetId($data);
return $res;
}
//4.多条插入
public function insertAll()
{
$data = [
['name' => '测试10','email' => 'cs10@qq.com','course' => 'php','grade' => '82','create_time' => time(),'update_time' => time(),],
['name' => '测试11','email' => 'cs11@qq.com','course' => 'php','grade' => '83','create_time' => time(),'update_time' => time(),],
['name' => '测试12','email' => 'cs12@qq.com','course' => 'php','grade' => '84','create_time' => time(),'update_time' => time(),],
];
// $res = Db::table('student')->insertAll($data);
$res = Db::table('student')->data($data)->insertAll();
return $res;
}
//5.更新操作
public function update($id = 1)
{
//更新一定要有更新条件,成功的话,返回更新的数量,失败的话返回false
// $res = Db::table('student')
// ->where('id',$id)
// ->update(['name'=>'李九','grade'=>0]);
//如果更新条件是主键,可以把主键写到更新数组中
$res = Db::table('student')
->update(['name'=>'李九','grade'=>0,'id'=>16]);
return $res;
}
//6.删除操作
public function delete($id = null)
{
if ($id == null){
return 'id参数有误';
}
$res = Db::table('student')->delete($id);
return $res;
}
//7.原生查询
public function query()
{
$sql = 'SELECT `name`,`email` FROM `student` WHERE `id` IN (4,5,6)';
dump(Db::query($sql));
}
//8.原生写操作:更新、删除、添加
public function execute()
{
// return Db::execute("UPDATE `student` SET `name`='武松' WHERE `id`=1");
// return Db::execute("INSERT `student` SET `name`='武松1'");
return Db::execute("DELETE FROM`student` WHERE `name`='武松1'");
}
}