文章目录
操作数据库
官方提供两种操作数据库的方法,使用Db类和助手函数db()。使用Db类时,需要指定命名空间use think\Db,使用助手函数时,可以不用指定命名空间。以下以mysql数据库为例进行测试练习。
创建数据库
- 建一个数据库,命名为
tp5 - 出入一个数据表
tp_data,建表sql语句如下
DROP TABLE IF EXISTS `tp_data`;
CREATE TABLE `tp_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`create_time` varchar(255) DEFAULT NULL,
`type` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of tp_data
-- ----------------------------
INSERT INTO `tp_data` VALUES ('1', 'xiao1', '1573887741', '0');
INSERT INTO `tp_data` VALUES ('2', 'xiao1', '1573887741', '0');
INSERT INTO `tp_data` VALUES ('3', 'xiao1', '1573887741', '0');
连接数据库配置
- 打开配置项文件
database.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'tp5',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
// 端口
'hostport' => '3306',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'tp_',
// 数据库调试模式
'debug' => true,
];
Db类执行原生sql
Db::execute()
- 执行增加、修改、删除操作
- 返回影响行数
<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$time = time();
$rel = Db::execute("INSERT INTO `tp_data`(`id`,`name`,`create_time`) VALUES (null,'xiaode',{$time}),(null,'rufeike',{$time})");
dump($rel);
}
}
?>
输出结果
int(2)
DB::query()
- 查询操作
- 返回关联数组结果集
<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$time = time();
$data = Db::query("SELECT * FROM `tp_type`");
dump($data)
}
}
?>
输出结果
array(8) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(4) "lldd"
["create_time"] => string(12) "154545415454"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(6) "xiaode"
["create_time"] => string(10) "1573876216"
}
}
Db类链式操作
Db::table()和DB::name()
- 指定数据库表面
- Db::table()需要指定表全名,Db::name()省略表前缀;
<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$table = Db::table('tp_data');
$table = Db::name('data');
}
}
db()助手函数
- 实例Db类,返回Db类对象
- 作用等同
Db::table()和Db::name()
<?php
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$db = db();
$rel= db('data')->insertGetId(array('name'=>'xiao','create_time'=>time()));
dump($rel);
}
}
输出结果
string(2) "16"
insert()出入单条据
- 链式操作末端方法,插入单条数据
- 参数格式
array('字段1'=>'字段值2','字段2'=>'字段值2'...) - 返回影响行数
<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$rel= Db::table('tp_data')->insert(array('name'=>'xiao','create_time'=>time()));
dump($rel);
}
}
输出结果
int(1)
insertGetId()插入单条据
- 链式操作末端方法,插入单条数据并且获得最后插入的只增主键值
- 参数格式
array('字段1'=>'字段值2','字段2'=>'字段值2'...)
<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$rel= Db::table('tp_data')->insertGetId(array('name'=>'xiao','create_time'=>time()));
dump($rel);
}
}
输出结果
string(2) "15"
insertAll()插入多条数据
- 链式操作末端方法,一次性插入多条数据
- 参数格式:
array(array(‘字段1’=>‘字段值2’,‘字段2’=>‘字段值2’…),array('字段1'=>'字段值2','字段2'=>'字段值2'...)); - 返回影响行数
<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$data = array(
array('name'=>'xiao1','create_time'=>time()),
array('name'=>'xiao2','create_time'=>time()),
array('name'=>'xiao3','create_time'=>time()),
);
$rel= db('data')->insertAll($data);
dump($rel);
}
}
输出结果
int(3)
update()更改数据
-
链式操作末端方法,根据指定条件进行更新操作,或者跟新数据中指定只增主键的值
-
参数格式
array('字段1'=>'字段值2','字段2'=>'字段值2'...) -
返回影响行数
<?php
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
//方式一
$data = array('name'=>'xiao1','create_time'=>time());
$rel= db('data')->where('id',10)->update($data);
//方式二
$data = array('id'=>10,'name'=>'xiao1','create_time'=>time());
$rel= db('data')->update($data);
dump($rel);
}
}
输出结果
int(1)
delete()删除操作
- 根据条件删除数据
- 参数格式:
detele(主键)和detele(array(主键1,主键2,主键3...)) - 返回是否执行成功,成功返回1,失败返回0
<?php
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$rel = db('data')->where(array('id'=>array('eq',10)))->delete();//方式1
$rel = db('data')->where('id',10)->update($data);//方式2
$rel = db('data')->delete(11);//方式3
$rel = db('data')->delete(12,13);//方式4
dump($rel);
}
}
find()查找单条数据
- 链式末端方法,查找单条数据
- 参数格式:
find(主键) - 返回关联数组结构集
- 注意:如果数据库对应的表没有对应的自增主键,默认会查询数据库表的第一条数据。如果有自增主键,可以在
find()参数中指定主键
<?php
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$rel1 = db('data')->find();//方式1
$rel2 = db('data')->find(3);//方式2
$rel3 = db('data')->where('id','lt',5)->find();
dump($rel1);
dump($rel2);
dump($rel3);
}
}
输出结果
array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
select()查询多条数据
- 链式末端方法,根据条件查询多条数据
- 参数:
- 空参数,默认查询全部数
- 指定主键数组或主键,查询对应的数据
- 返回结果:二维数组
<?php
namespace app\index\controller;
use \think\Controller;
class Index extends Controller{
public function index(){
$rel1 = db('data')->select();//方式1
$rel2 = db('data')->select(array(1,2,3));//方式2
$rel3 = db('data')->where('id','lt',5)->select();//方式3
dump($rel1);
dump($rel2);
dump($rel3);
}
}
输出结果
array(19) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[2] => array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
...
}
array(3) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[2] => array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
}
array(4) {
[0] => array(3) {
["id"] => int(1)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[1] => array(3) {
["id"] => int(2)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[2] => array(3) {
["id"] => int(3)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
[3] => array(3) {
["id"] => int(4)
["name"] => string(5) "xiao1"
["create_time"] => string(10) "1573887741"
}
}
1673



被折叠的 条评论
为什么被折叠?



