1. ThinkPHP database operation
In ThinkPHP, we can operate the database through the database operation classes it provides. Commonly used database operation classes are:
Db class
In ThinkPHP, we can use the Db class to add and delete the database. , modify, check and other operations. Examples of its use are as follows:
<?php use think\Db; // 查询数据 $list = Db::table('user')->where('id', 1)->find(); // 新增数据 $data['name'] = 'test'; $data['age'] = 20; Db::table('user')->insert($data); // 更新数据 $where['id'] = 1; $data['name'] = 'test'; $data['age'] = 30; Db::table('user')->where($where)->update($data); // 删除数据 $where['id'] = 1; Db::table('user')->where($where)->delete();
Model class
In ThinkPHP, the Model class inherits from the Db class, so the Model class can use the Db class All methods, but also provides some more convenient methods. Examples of its use are as follows:
<?php namespace app\index\model; use think\Model; class User extends Model { // 查询数据 public function getUserById($id) { return $this->where('id', $id)->find(); } // 更新数据 public function updateUser($id, $name) { return $this->save(['name' => $name], ['id' => $id]); } }
2. How to modify the database with ThinkPHP
When using ThinkPHP to modify the database, you usually go through the following steps:
Create model
We need to create a model that corresponds to the database table we need to access. Since ThinkPHP adopts the MVC design pattern, we need to inherit the Model class from ThinkPHP when creating the model. The following is an example of creating a User model:
<?php namespace app\index\model; use think\Model; class User extends Model { protected $table = 'user'; protected $pk = 'id'; }
When creating the User model, we specified that the database table corresponding to the model is the user table, and the primary key of the table is id.
Instantiate the model
Next, we need to use the previously created User model to instantiate it and use this model to operate the database. Here is an example of instantiating the User model:
<?php $userModel = new \app\index\model\User();
Modify data
Once you create an instance of the User model, you can leverage the The provided functions make modifications to the database. The following is an example of using the User model to modify data:
<?php $userModel = new \app\index\model\User(); // 更新数据 $where['id'] = 1; $data['name'] = 'test'; $data['age'] = 30; $userModel->where($where)->update($data);
In the above example, we use the update() method of $UserModel to modify the data with id 1 in the User table, and change the row of data. Change the name field to test and the age field to 30.
The above is the detailed content of How to modify the database in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
