Home >PHP Framework >ThinkPHP >How to implement addition, deletion, modification and query in thinkphp3

How to implement addition, deletion, modification and query in thinkphp3

PHPz
PHPzOriginal
2023-04-17 10:28:50666browse

This article will introduce you to the addition, deletion, modification and search operations under the ThinkPHP3 framework. I hope it will be helpful to developers who are learning the ThinkPHP3 framework.

  1. Add operation

In ThinkPHP3, you can use the M method to add a piece of data, for example:

$Model = M('User');
$data['name'] = 'wuzhi';
$data['password'] = md5('123456');
$result = $Model->add($data);

The above code means to instantiate the User model , and set the name and password attributes for it, and then insert the data into the database through the add method. $result is the primary key value returned after insertion.

  1. Query operation

To query a single data, use the find method, to query multiple data, use the select method, for example:

$Model = M('User');
$condition['id'] = 1;
$result = $Model->where($condition)->find();

The above code means query id User data of 1 and return the result.

$Model = M('User');
$condition['id'] = array('between',array(1,10));
$result = $Model->where($condition)->select();

The above code queries user data with IDs between 1-10 and returns the results.

  1. Modification operation

Use the save method to modify existing data, for example:

$Model = M('User');
$condition['id'] = 1;
$data['name'] = 'lisi';
$data['password'] = md5('654321');
$result = $Model->where($condition)->save($data);

The above code will change the name and The password is changed to lisi and 654321.

  1. Delete operation

Use the delete method to delete data, for example:

$Model = M('User');
$condition['id'] = 1;
$result = $Model->where($condition)->delete();

The above code deletes the user data with id 1.

The above is a basic introduction to the addition, deletion, modification and search operations under the ThinkPHP3 framework. In actual development, we need to flexibly use the above methods to achieve various functions. I hope this article can help everyone.

The above is the detailed content of How to implement addition, deletion, modification and query in thinkphp3. 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