MySQL의 마지막 PK값 획득.. mysql_insert_id() 와 last_insert_id() > 기술자료

본문 바로가기

사이트 내 전체검색

MySQL의 마지막 PK값 획득.. mysql_insert_id() 와 last_insert_id() > 기술자료

PHP MySQL의 마지막 PK값 획득.. mysql_insert_id() 와 last_insert_id()

페이지 정보


본문

DB에 데이터를 입력하면서 입력한 바로 그 값의 uid을 가져다가 써야 할 때가 있습니다.
2가지 방법이 있는데 mysql_insert_id() 과 last_insert_id() 입니다.

mysql_insert_id()은 PHP에서 처리해주는 명령문이고 last_insert_id()는 MySQL에서 처리해 주는 함수 입니다.
INSERT 명령으로 입력된 바로 그값의 PK(Primary Key)를 가져오는 명령을 수행합니다.


1. PHP 구문
$query = "INSERT INTO table (field1, field2) VALUES (value1, value2)";
$result = mysql_query($query);
if ($result)
$last_uid = mysql_insert_id();


2. MySQL 구문
$query = "INSERT INTO table (field1, field2) VALUES (value1, value2)";
$result = mysql_query($query);
if ($result)
$last_uid = mysql_query("last_insert_id()");


3. mysql_insert_id 
(PHP 3, PHP 4 , PHP 5) 
mysql_insert_id --  최근 INSERT 작업으로부터 생성된 identifier 값을 반환 

설명 
int mysql_insert_id ( [int link_identifier]) 

mysql_insert_id()는 link_identifier를 사용하면, INSERT 질의로 행(row)를 추가한 뒤 AUTO_INCREMENT로 생성된 테이블의 컬럼에 추가된 값을 얻을 수 있다. 
link_identifier를 지정하지 않으면, 마지막에 열려진 link를 사용한다. 

mysql_insert_id()는 이전 질의가 AUTO_INCREMENT값으로 생성되지 않으면, 0을 반환한다. 마지막으로 저장된 값이 필요하다면, 질의로 값을 추가한 직후 mysql_insert_id()를 사용하면 된다. 

참고: LAST_INSERT_ID()는 AUTO_INCREMENT 값으로 생성된 가장 최근 값이 보관되며, 질의할 동안에는 없어지지 않는다. 


4. mysqli_insert_id
(PHP 5, PHP 7)
mysqli::$insert_id -- mysqli_insert_id — Returns the auto generated id used in the latest query


5. 그누보드 5.1.3 이후 mysqli 함수 추가
그누보드5 / lib / common.lib.php
<?php
function sql_insert_id($link=null)
{
    global $g5;

    if(!$link)
        $link = $g5['connect_db'];

    if(function_exists('mysqli_insert_id') && G5_MYSQLI_USE)
        return mysqli_insert_id($link);
    else
        return mysql_insert_id($link);
}
?>


참고자료
http://jos39.tistory.com/191
http://unabated.tistory.com/entry/mysqlinsertid
https://dev.mysql.com/doc/refman/5.7/en/mysql-insert-id.html
https://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=36144
http://php.net/manual/en/function.mysql-insert-id.php

댓글목록

등록된 댓글이 없습니다.


Total 200건 3 페이지
  • RSS
기술자료 목록
160
HTML   16546  2017-09-28 12:23 ~ 2019-10-10 23:30  
159
HTML   32015  2017-09-18 23:09  
158
HTML   28844  2017-09-05 04:39  
157
HTML   12309  2017-07-27 11:39  
156
HTML   12944  2017-07-10 14:33  
155
HTML   35038  2017-04-19 21:50  
154
HTML   11688  2017-04-18 11:48  
153
HTML   11155  2016-12-06 02:42  
152
HTML   12326  2016-11-24 16:58  
151
HTML   9633  2016-11-15 03:17 ~ 2016-12-27 00:00  
150
HTML   14616  2016-10-06 11:49  
149
HTML   14762  2016-09-09 00:58 ~ 2016-09-20 00:00  
148
HTML   16012  2016-09-03 18:38  
147
HTML   15957  2016-08-10 16:38  
146
HTML   13645  2016-08-09 14:29  
145
HTML   18836  2016-08-09 11:33  
144
HTML   19492  2016-08-09 00:33  
143
HTML   14500  2016-08-09 00:29 ~ 2017-04-14 00:00  
142
HTML   14477  2016-08-06 17:41 ~ 2018-08-01 07:10  
141
HTML   18317  2016-02-21 06:31  

검색

해피정닷컴 정보

회사소개 회사연혁 협력사 오시는길 서비스 이용약관 개인정보 처리방침

회사명: 해피정닷컴   대표: 정창용   전화: 070-7600-3500   팩스: 042-670-8272
주소: (34368) 대전시 대덕구 대화로 160 대전산업용재유통단지 1동 222호
개인정보보호책임자: 정창용   사업자번호: 119-05-36414
통신판매업신고: 제2024-대전대덕-0405호 [사업자등록확인]  
Copyright 2001~2026 해피정닷컴. All Rights Reserved.