Home > Article > PHP Framework > How to add data to thinkphp database
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.
#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);
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();
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);
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);
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);
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);
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);
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();
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();
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!