
mysqli_stmt::$insert_id를 사용하여 마지막으로 INSERT된 ID를 얻는 방법은 다음과 같습니다.
mysqli_stmt::$insert_id는 INSERT 문을 실행하고 나면 마지막으로 INSERT된 ID를 반환합니다. 하지만, INSERT 문을 여러 번 실행한 경우 마지막으로 INSERT된 ID를 얻기 위해서는 mysqli_stmt::$insert_id를 다시 초기화하는 방법을 사용해야 합니다.
mysqli_stmt::$insert_id를 초기화하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$stmt = $mysqli->prepare("INSERT INTO 테이블명 (컬럼명) VALUES (?)");
$stmt->execute();
$mysqli->next_result();
$last_id = $mysqli->insert_id;
위의 예제에서 $mysqli->next_result() 함수를 사용하여 mysqli_stmt::$insert_id를 초기화하고, $mysqli->insert_id를 사용하여 마지막으로 INSERT된 ID를 얻을 수 있습니다.
또한, mysqli_stmt::$insert_id를 초기화하는 방법으로는 mysqli_stmt::$insert_id = 0을 사용할 수도 있습니다.
#hostingforum.kr
php
$stmt = $mysqli->prepare("INSERT INTO 테이블명 (컬럼명) VALUES (?)");
$stmt->execute();
$stmt->insert_id = 0;
$last_id = $stmt->insert_id;
위의 예제에서 stmt->insert_id = 0을 사용하여 mysqli_stmt::$insert_id를 초기화하고, stmt->insert_id를 사용하여 마지막으로 INSERT된 ID를 얻을 수 있습니다.
mysqli_stmt::$insert_id를 초기화하는 방법은 위의 예제를 참고하여 사용하시기 바랍니다.
2025-04-13 13:21