Home > PHP Framework > ThinkPHP > body text

How to use thinkphp5 db class

WBOY
Release: 2023-05-31 14:58:06
forward
1148 people have browsed it

1. Connect to the database

There are two ways to connect to the database, one is to configure it in config.php, the other is to instantiate Db Pass in connection parameters when class.

Configure in config.php:

return [
    // 数据库类型
    'type'      => 'mysql',
    // 服务器地址
    'hostname'  => '127.0.0.1',
    // 数据库名
    'database'  => 'test',
    // 数据库用户名
    'username'  => 'root',
    // 数据库密码
    'password'  => '',
    // 数据库连接端口
    'hostport'  => '',
];
Copy after login

Pass in the connection parameters when instantiating the Db class:

use think\Db;

$config = [
    // 数据库类型
    'type'      => 'mysql',
    // 服务器地址
    'hostname'  => '127.0.0.1',
    // 数据库名
    'database'  => 'test',
    // 数据库用户名
    'username'  => 'root',
    // 数据库密码
    'password'  => '',
    // 数据库连接端口
    'hostport'  => '',
];

Db::connect($config);
Copy after login

2. Basic operations

1. Query operation

use think\Db;

//查询一条数据
Db::table('user')->where('id', 1)->find();

//查询多条数据
Db::table('user')->where('age', '>', 18)->select();
Copy after login

2. Insert operation

use think\Db;

$data = [
    'username' => 'admin',
    'password' => md5('admin'),
    'sex'      => 1,
    'age'      => 20,
];

Db::table('user')->insert($data);
Copy after login

3. Update operation

use think\Db;

Db::table('user')->where('id', 1)->update(['age' => 21]);
Copy after login

4. Delete operation

use think\Db;

Db::table('user')->where('id', 1)->delete();
Copy after login

3. Advanced operations

1. Chain operation

Chain operation can simplify the writing of sql statements.

use think\Db;

Db::table('user')
    ->alias('u')
    ->join('role r', 'u.role_id=r.id')
    ->where('u.id', 1)
    ->field('u.username, r.name')
    ->find();
Copy after login

2. Debugging methods

In the development environment, we often need to check the execution of sql statements. The Db class provides three debugging methods: getLastSql, getExplain and getSqlLog.

use think\Db;

Db::table('user')->getLastSql();

Db::table('user')->where('age', '>', 18)->getExplain();

Db::table('user')->where('age', '>', 18)->select();
Db::table('user')->getLastSql();

print_r(Db::getSqlLog());
Copy after login

The getLastSql method can obtain the last executed sql statement.

The getExplain method can obtain the execution plan of the sql statement.

The getSqlLog method can obtain all executed sql statements and execution time.

The above is the detailed content of How to use thinkphp5 db class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template