Home  >  Article  >  PHP Framework  >  How to connect data with thinkphp

How to connect data with thinkphp

PHPz
PHPzOriginal
2023-05-26 09:04:361367browse

ThinkPHP is an open source Web framework written in PHP, which is easy to learn, efficient, fast, safe and stable. When developing with ThinkPHP, you need to connect to a database to store and manage data. This article will introduce how to connect to the database and perform basic operations.

1. Configure database connection information

In ThinkPHP, you need to configure the database connection information in the public configuration file config.php. In the config.php file, find the following code:

//数据库类型
'DB_TYPE'   => 'mysql',
//服务器地址
'DB_HOST'   => 'localhost',
//数据库名
'DB_NAME'   => 'thinkphp',
//用户名
'DB_USER'   => 'root',
//密码
'DB_PWD'    => 'root',
//端口
'DB_PORT'   => '3306',
//表前缀
'DB_PREFIX' => 'think_',

Among them, DB_TYPE is the database type, which currently supports mysql, mysqli, PDO and other types; DB_HOST is the database server address, which can be an IP address or domain name; DB_NAME is the database name, which needs to be created before connecting; DB_USER and DB_PWD are the user name and password of the database respectively, which require permission to access the database; DB_PORT is the port for database connection, the default is 3306; DB_PREFIX is the data table prefix, used for multiple Avoid table name conflicts when two applications share a database.

2. Connect to the database

After completing the configuration of the database connection information, you can connect to the database by instantiating a database object. In ThinkPHP, use the Db class to connect and operate the database. For example:

use thinkDb;

// 连接数据库
$conn = Db::connect();

If you need to specify the database configuration of the connection, you can pass an array parameter in the connect method, and the array contains the database connection information. For example:

$config = [
    //数据库类型
    'type'     => 'mysql',
    //服务器地址
    'hostname' => 'localhost',
    //数据库名
    'database' => 'thinkphp',
    //用户名
    'username' => 'root',
    //密码
    'password' => 'root',
    //端口
    'hostport' => '',
    //表前缀
    'prefix'   => 'think_',
];

// 连接数据库
$conn = Db::connect($config);

3. Database operations

After connecting to the database, you can perform database operations, including queries, inserts, updates, and deletes. The following takes the query operation as an example for explanation.

  1. Query a record

Use the query method to query a record, for example:

// 查询一条记录
$record = $conn->query('select * from think_user limit 1');

The query method returns a PDOStatement object, which can be used through the fetch method Get a record.

// 获取查询结果
$row = $record->fetch(PDO::FETCH_ASSOC);
  1. Query multiple records

Use the select method to query multiple records, for example:

// 查询多条记录
$list = $conn->table('think_user')->select();

The table method is used to set the data for the operation Table, select method is used to obtain multiple records.

  1. Conditional query

You can set query conditions through the where method, for example:

// 条件查询
$list = $conn->table('think_user')->where('id', '=', 1)->select();

Among them, the where method receives three parameters, which are field names. , comparison operators and values, multiple conditional queries can be implemented through chain operations. For example:

// 多条件查询
$list = $conn->table('think_user')
                ->where('id', '=', 1)
                ->where('status', '=', 1)
                ->select();
  1. Insert record

Use the insert method to insert a record, for example:

// 插入记录
$data = [
    'username' => 'admin',
    'password' => md5('123456'),
    'status'   => 1,
];
$result = $conn->table('think_user')->insert($data);

The insert method receives an array parameter, in the array Contains field names and corresponding values.

  1. Update records

Use the update method to update records, for example:

// 更新记录
$data = ['status' => 0];
$result = $conn->table('think_user')
                ->where('id', '=', 1)
                ->update($data);

The update method receives an array parameter, and the array contains the Field name and corresponding value.

  1. Delete records

Use the delete method to delete records, for example:

// 删除记录
$result = $conn->table('think_user')
                ->where('id', '=', 1)
                ->delete();

Among them, the delete method can delete multiple records that meet the conditions and does not pass Conditional parameters will clear the entire table.

4. Summary

Through the above steps, you can successfully connect to the database and perform basic query, insert, update and delete operations. When developing with ThinkPHP, you need to frequently deal with the database, so it is very important to be proficient in database operations. At the same time, you need to pay attention to security issues such as SQL injection when operating, and use parameter binding and other methods as much as possible to avoid risks.

The above is the detailed content of How to connect data with thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to define thinkphpNext article:How to define thinkphp