Home > PHP Framework > ThinkPHP > body text

How to add data to thinkphp database

藏色散人
Release: 2022-01-10 11:36:38
Original
4349 people have browsed it

How to add data to thinkphp database: 1. Add a piece of data through the insert method; 2. Use the data method in conjunction with insert to add a piece of data; 3. Pass in multiple pieces of data to the insertAll method of the Db class.

How to add data to thinkphp database

#The operating environment of this article: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.

How to add data to thinkphp database?

Add a piece of data

Use the insert method of the Db class to submit data to the database

$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::name('user')->insert($data);
Copy after login

The insert method successfully returns the number of successfully added data. Normally, it returns 1

or uses the data method in conjunction with insert.

$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::name('user')
    ->data($data)
    ->insert();
Copy after login

If there is no foo or bar field in your data table, an exception will be thrown.

If you do not want to throw an exception, you can use the following method:

$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::name('user')->strict(false)->insert($data);
Copy after login

The value of the non-existent field will be discarded directly.

If it is a mysql database, replace writing is supported, for example:

$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::name('user')->insert($data, true);
Copy after login

After adding data, if you need to return the auto-incremented primary key of the new data, you can use the insertGetId method to add data and return the primary key. Value:

$userId = Db::name('user')->insertGetId($data);
Copy after login

insertGetId method adds data successfully and returns the auto-incremented primary key of the added data

Add multiple pieces of data

Add multiple pieces of data directly to Db The insertAll method of the class can pass in the data that needs to be added.

$data = [
    ['foo' => 'bar', 'bar' => 'foo'],
    ['foo' => 'bar1', 'bar' => 'foo1'],
    ['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->insertAll($data);
Copy after login

The insertAll method successfully adds data and returns the number of successfully added items.

If it is a mysql database, replace writing is supported, for example:

$data = [
    ['foo' => 'bar', 'bar' => 'foo'],
    ['foo' => 'bar1', 'bar' => 'foo1'],
    ['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->insertAll($data, true);
Copy after login

You can also use the data method

$data = [
    ['foo' => 'bar', 'bar' => 'foo'],
    ['foo' => 'bar1', 'bar' => 'foo1'],
    ['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->data($data)->insertAll();
Copy after login

Ensure that the data fields to be added in batches are consistent

If there is a lot of data to be inserted in batches, you can specify batch insertion and use the limit method Specify the quantity limit for each insert.

$data = [
    ['foo' => 'bar', 'bar' => 'foo'],
    ['foo' => 'bar1', 'bar' => 'foo1'],
    ['foo' => 'bar2', 'bar' => 'foo2']
    ...
];
// 分批写入 每次最多100条数据
Db::name('user')->data($data)->limit(100)->insertAll();
Copy after login

Recommended learning: "The latest 10 thinkphp video tutorials"

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

Related labels:
source:php.cn
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