连接数据库
1、全局配置
在config目录下的database.php文件中配置
2、动态配置
使用think\db\Query.php
中的connect()
方法
实例:查询wish表中id为1的name属性值
public function conn1()
{
return Db::connect([
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'php_wish',
// 用户名
'username' => 'root',
// 密码
'password' => '123456',
])
->table('wish')->where('id', 1)->value('name');
}
DSN连接
格式: 用户名:密码@数据库地址:端口号/数据库名称#字符集
public function conn2()
{
$dsn = 'mysql://root:123456@127.0.0.1:3306/php_wish#utf8';
return Db::connect($dsn)->table('wish')->where('id', 2)->value('name');
}
增删改查
查询构造器
准备工作:
修改config目录下的app.php
app_debug=>'true';
app_trace=>'true';
查询
要先连接数据库,可以在database.php
种进行配置
单条查询
//单条查询
public function find()
{
/**
* table() :选择数据表
* where() :设置查询条件 ,表达式和数组两种形式。对应单个条件使用表达式,多个条件使用数组
* find() :返回符合条件的第一条记录,没有时返回null
* filed() :指定返回那些字段
*/
$res = Db::table('wish')
->where('id', '=', 1)
->find();
dump(is_null($res) ? '没找到' : $res);
}
//指定返回那些字段
$res = Db::table('wish') ->field('id,name,content')->where('id', '=', 1) ->find();
dump(is_null($res) ? '没找到' : $res);
//where函数,如果是相等关系,等号可省略
->where('id', 1)
//如果是主键查询,where条件可省略
$res = Db::table('wish') ->field('id,name,content') ->find(2);
多条查询
/**
* 多条查询返回一个二维数组,没有数据返回空数组
*/
$res = Db::table('wish')->field('id,name,content')
->where([
['id', '<', '10'],
['color', '=', 'red']
])->select();
if (empty($res)) {
return "没有符合条件的记录";
} else {
var_dump($res);
}
}
单条插入
//单条插入
public function insert()
{
//成功返回新增的数量,失败返回false
//准备要插入的数据
$data = [
'id' => '7',
'name' => 'Java',
'content' => '努力成为Java全栈工程师',
'time' => time(),
'color' => 'red'
];
//方式一
return Db::table('wish')->insert($data);
//方式二
return Db::table('wish')->data($data)->insert();
//方式三
//插入的同时返回新增主键id
return Db::table('wish')->insertGetId($data);
}
多条插入
//多条插入
public function insertAll()
{
$data = [
[
'id' => '10',
'name' => '李淳罡',
'content' => '天不生我李淳罡,万古剑道长如夜',
'time' => time(),
'color' => 'red'
],
[
'id' => '12',
'name' => '李白',
'content' => '长风破浪会有时,直挂云帆济沧海',
'time' => time(),
'color' => 'blue'
]
];
//方式一
return Db::table('wish')->insertAll($data);
//方式二
return Db::table('wish')->data($data)->insertAll();
}
更新
//更新
public function update()
{
//更新操作必须有条件,成功返回更新的数量,失败返回false
return DB::table('wish')->where('id', 5)
->update(['name' => '王五', 'content' => '我自横刀向天笑,去留肝胆两昆仑']);
}
删除
//删除
public function delete()
{
/**
* 成功返回删除的数量,失败返回0
* 当以主键为删除条件时,可以省略where
*/
//$res = Db::table('wish')->where('id', '=', 13)->delete();
$res = Db::table('wish')->delete(12);
dump($res);
}
助手函数
以上操作可以使用助手函数进行操作,以查询为例
public function find()
{
$res = db('wish')
->field('id,name,content')
->find(2);
dump(is_null($res) ? '没找到' : $res);
}
原生操作
查询
public function query()
{
$sql = "select name,content from wish where id in (3,4,5)";
dump(Db::query($sql));
}
写操作(更新、插入、删除)
public function execute()
{
$sql = "update wish set name='张飞' where id=1";
$res = Db::execute($sql);
dump($res);
}
模型
要点:
- 模型类要与要绑定的数据表同名。模型会自动对应数据表,模型类的命名规则是除去表前缀的数据表名称,采用驼峰法命名,并且首字母大写
- 模型类要继承公共模型类
use think\Model;
- 使用php模型类时要引入模型类
- 使用模型前要保已经在数据库配置文件中配置了数据库连接信息
模型类
<?php
namespace app\index\model;
//继承公共模型类
use think\Model;
//模型类要与要绑定的数据表同名
class Wish extends Model{
}
实现类
<?php
namespace app\index\controller;
//引入对应的模型
use app\index\model\Wish;
//模型是和一张数据表绑定的
class Demo6
{
// 单条查询 取出主键为3的数据
public function get()
{
//方式一
// dump(Wish::get(3));
//方式二:使用查询构造器
$res = Wish::field('id,name,content')
->where('id', 3)
->find();
dump($res);
}
//多条查询 取出主键为1,2,3的数据
public function all()
{
//方式一
$list = Wish::all('1,2'); //或 Wish::all([1,2])
dump($list);
//方式二 查询构造器
$list = Wish::field('id,name')
->where('id', 'in', '1,2,3')->select();
dump($list);
}
}
注:Wish:: 就相当于 Db::table(‘wish’)
插入
//单条插入
public function insert()
{
//方式一
$wish = new Wish();
$wish->id = 12;
$wish->name = '孙悟空';
$wish->econtent = '妖孽哪里逃';
$wish->time = time();
$wish->save();
//方式二
$wish = new Wish();
$wish->save(
[
'id' => 11,
'name' => '阿离',
'content' => '有缘又有聚,才是阿离的舞台',
'time' => time()
]
);
//方式三
$user = new User([
'name' => 'thinkphp',
'email' => 'thinkphp@qq.com'
]);
$user->save();
}
//多条插入
$user = new User;
$list = [
['name'=>'thinkphp','email'=>'thinkphp@qq.com'],
['name'=>'onethink','email'=>'onethink@qq.com']
];
$user->saveAll($list);
saveAll
方法新增数据默认会自动识别数据是需要新增还是更新操作,当数据中存在主键的时候会认为是更新操作
其它详细操作,见开发手册
传送门