Home >Backend Development >PHP Tutorial >THINKPHP method to obtain the value of the primary key id when adding data

THINKPHP method to obtain the value of the primary key id when adding data

不言
不言Original
2018-05-04 11:37:462420browse

This article mainly introduces the method of THINKPHP to obtain the value of the primary key id when adding data. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

When using ThinkPHP After adding new data, you can easily obtain the automatically growing primary key value.

$Model = D(‘Blog');

$data['name'] = 'test';

$data['title'] = '测试标题';

$data['content'] = '测试内容';

$result = $Model->add($data);

if ($result){

  $id = $result; // 获取数据库写入数据的主键

}else{

  exit($Model->getError());

}

If the primary key of your data table is auto-growing, then the return value after the add method succeeds is the primary key value of the data. No additional acquisition required.

Related recommendations:

ThinkPHP method of adding update tag_php example

# #

The above is the detailed content of THINKPHP method to obtain the value of the primary key id when adding data. 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