Home  >  Article  >  Backend Development  >  ThinkPHP3.2 framework uses addAll() to insert data in batches

ThinkPHP3.2 framework uses addAll() to insert data in batches

不言
不言Original
2018-06-07 16:22:492640browse

This article mainly introduces the method of batch inserting data using addAll() in the ThinkPHP3.2 framework, and analyzes thinkPHP's related implementation skills for single data insertion and batch data insertion operations in the form of examples. Friends in need can refer to the following

The example of this article describes the method of batch inserting data using addAll() in the ThinkPHP3.2 framework. Share it with everyone for your reference, the details are as follows:

The addAll() method of the model class in thinkphp can add data to the database at the same time.

// 批量添加数据 (only MySQL)
$user = M('user');
//array('表字段'=>'值')
$dataList[] = array('name'=>'thinkphp','email'=>'thinkphp@gamil.com');
$dataList[] = array('name'=>'onethink','email'=>'onethink@gamil.com');
$insertOkInfo = $user->addAll($dataList);

The following is the method of inserting a single piece of data

$user = M('demo');
$data['name'] = 'xiaoming';
$data['sex'] = '1';
$data['age'] = '23';
// 使用add()方法将数据写入数据库
// 返回 Id
$insertId = $user->add($data);

There is also a practical methodfilter() ,This method is to filter the field content into text.

The following example:

Converta4b561c25d9afb9ac8dc4d70affff419thinkphp0d36329ec37a2cc24d42c7229b69747a into "thinkphp"

//name字段有html标签
$data[&#39;name&#39;] = &#39;<b>thinkphp</b>&#39;;
$data[&#39;sex&#39;] = &#39;1&#39;;
$User = M(&#39;demo&#39;);
// 写入数据库的时候会把name字段的值<b>thinkphp</b>转化为“thinkphp”
$User->data($data)->filter(&#39;strip_tags&#39;)->add();

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Thinkphp5.0's method of automatically generating modules and directories

ThinkPHP framework implements session cross-domain issues

Perfect solution to the problem of inserting the same data in Thinkphp3.2

The above is the detailed content of ThinkPHP3.2 framework uses addAll() to insert data in batches. 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