MySQL의 마지막 PK값 획득.. mysql_insert_id() 와 last_insert_id() > 기술자료 | 해피정닷컴

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 196건 1 페이지
  • RSS
기술자료 목록
196
PHP   838  2025-02-07 09:27 ~ 2025-02-07 16:59  
195
PHP   2347  2024-02-06 16:42  
194
PHP   1831  2024-01-26 11:04 ~ 2024-01-26 11:13  
193
PHP   3566  2023-11-20 10:56 ~ 2023-11-20 11:14  
192
PHP   4380  2023-04-14 18:22 ~ 2023-04-14 18:40  
191
PHP   4332  2022-11-16 18:17  
190
PHP   5140  2022-11-15 11:55 ~ 2022-11-18 17:26  
189
PHP   4895  2022-10-13 20:59 ~ 2022-10-14 09:44  
188
PHP   3929  2022-10-06 14:29  
187
PHP   3476  2022-10-06 12:20 ~ 2022-10-06 12:29  
186
PHP   4557  2022-05-20 12:09 ~ 2024-03-21 15:12  
185
PHP   7541  2022-02-27 03:45 ~ 2022-03-05 01:33  
184
PHP   6260  2021-12-20 20:32 ~ 2021-12-22 18:59  
183
PHP   7305  2021-11-09 17:52 ~ 2021-11-09 17:57  
182
PHP   7202  2021-06-30 10:40  
181
PHP   8539  2021-03-04 19:10 ~ 2021-06-22 00:03  
180
PHP   7231  2021-03-03 16:20 ~ 2021-03-03 16:21  
179
PHP   12236  2021-01-20 12:07 ~ 2021-01-27 13:35  
178
PHP   7589  2020-11-08 13:01 ~ 2020-11-08 13:03  
177
PHP   8798  2020-11-06 20:08  

검색

해피정닷컴 정보

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

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