---
id: php-mysql-insert-last-id
title: "PHP MySQL Insert Last ID"
category: "Programming_Language"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["insert_id", "lastInsertId", "PHP MySQL 마지막 ID"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.89
created_at: 2026-07-04
updated_at: 2026-07-04
review_reason: ""
merge_history: []
tags: ["php", "programming", "w3schools", "mysql", "insert", "auto-increment"]
raw_sources: ["https://www.w3schools.com/php/php_mysql_insert_lastid.asp"]
applied_in: []
github_commit: ""
---

# [[PHP MySQL Insert Last ID]]

## 🎯 한 줄 통찰 (One-line insight)
Retrieving the auto-generated ID uses three completely different syntax forms across the three connection APIs — `$conn->insert_id` (MySQLi OOP property), `mysqli_insert_id($conn)` (MySQLi procedural function), and `$conn->lastInsertId()` (PDO method) — meaning the same conceptual operation has zero syntax overlap between APIs. [S1]

## 🧠 핵심 개념 (Core concepts)
- **`$conn->insert_id`** (MySQLi OOP) — property holding the last AUTO_INCREMENT value generated. [S1]
- **`mysqli_insert_id($conn)`** (MySQLi procedural) — the equivalent function form. [S1]
- **`$conn->lastInsertId()`** (PDO) — the equivalent method form. [S1]
- **Only meaningful after INSERT/UPDATE on an AUTO_INCREMENT field** — retrieves the ID assigned during that specific operation. [S1]

## 📖 세부 내용 (Details)
- MySQLi OOP: `if ($conn->query($sql) === TRUE) { $last_id = $conn->insert_id; echo "Last inserted ID is: " . $last_id; }`. [S1]
- MySQLi procedural: `if (mysqli_query($conn, $sql)) { $last_id = mysqli_insert_id($conn); echo "Last inserted ID is: " . $last_id; }`. [S1]
- PDO: `$conn->exec($sql); $last_id = $conn->lastInsertId(); echo "Last inserted ID is: " . $last_id;`. [S1]

## ⚖️ 모순 및 업데이트 (Contradictions & updates)
소스에서 모순되는 정보는 발견되지 않음.

## 🛠️ 적용 사례 (Applied in summary)
현재 발견된 실제 적용 사례가 없습니다 — 새로 삽입된 레코드의 ID를 즉시 확인해 후속 작업(예: 관련 레코드 연결)에 사용하는 것이 실전 활용의 대표 사례다. [S1]

## 💻 코드 패턴 (Code patterns)
Retrieving the last inserted ID across three APIs (PHP):
```php
// MySQLi OOP:
$last_id = $conn->insert_id;
// MySQLi procedural:
$last_id = mysqli_insert_id($conn);
// PDO:
$last_id = $conn->lastInsertId();
```

## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.89
- **중복 검사 결과:** 신규 생성 (New discovery)

## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[PHP Tutorial]]
- **관련 개념:** [[PHP MySQL Insert]], [[PHP MySQL Insert Multiple]]
- **참조 맥락:** 삽입 직후 ID 조회 — 다중 레코드 삽입(Insert Multiple) 챕터로 이어짐.

## 📚 출처 (Sources)
- [S1] W3Schools — PHP MySQL Get Last Inserted ID — https://www.w3schools.com/php/php_mysql_insert_lastid.asp

## 📝 변경 이력 (Change history)
- 2026-07-04: Initial draft synthesized from the W3Schools "PHP MySQL Get Last Inserted ID" page (Astra wiki-curation, P-Reinforce v3.1 format).
