mysql_insert_id(), last_insert_id()

유영·2023년 6월 19일
0

MYSQL

목록 보기
19/20

MySQL의 마지막 PK값 획득

MySQL의 마지막 PK값 획득은 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()");

0개의 댓글