Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to increase data in thinkphp Database

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report