Get the App
SLTechnology News&Howtos  ›  Development  › 

How to increase data in thinkphp Database

Shulou Source: shulou.com Published: 2022-06-02 04:39:43 09月12日 Update

Editor to share with you how the thinkphp database to increase data, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

The method of adding data to the thinkphp database: 1, add a piece of data through the insert method; 2, use the data method with insert to add a piece of data; 3, pass multiple pieces of data into the insertAll method of the Db class.

This article operating environment: Windows7 system, thinkphp v5.1, Dell G3 computer.

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 entries that have been successfully added, usually 1

Or use the data method with insert.

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

If there are no foo or bar fields in your datasheet, 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 values of fields that do not exist will be discarded directly.

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

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

After adding data, if you need to return the self-increasing primary key of the newly added data, you can use the insertGetId method to add the data and return the primary key value:

$userId = Db::name ('user')-> insertGetId ($data)

The insertGetId method successfully returns the self-increasing primary key of adding data.

Add multiple pieces of data

To add multiple pieces of data, you can pass the data to be added directly to the insertAll method of the Db class.

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

The number of successful entries returned by the insertAll method for adding data successfully

If it is a mysql database, replace writes are 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 you have a lot of data to insert in bulk, you can specify a batch insert and use the limit method to specify a limit on the number of inserts each time.

$data = [['foo' = >' bar', 'bar' = >' foo'], ['foo' = >' bar1', 'bar' = >' foo1'], ['foo' = >' bar2', 'bar' = >' foo2'].]; / / write a maximum of 100 data per batch Db::name ('user')-> data ($data)-> limit (100)-> insertAll () The above is all the contents of the article "how to add data in thinkphp Database". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Data method database success multiple field article plus one content support consistency not so much most situation datasheet quantity more environment computer knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno Linux Shulou Technology vpn