如何使用预准备语句获取最后插入的查询的id?
我写了一些PHP,但结果只得到"0".我试图用这个问题的答案:关于SO的类似问题
$locationName = $_GET['locationName']; $locationResume = $_GET['locationResume']; $sql = "INSERT INTO location (locationTitle, locationResume) VALUES (?,?);"; if ($locationName != null && $locationResume != null ) { if ($stmt = $con->prepare($sql)) { $stmt->bind_param("ss", $locationName, $locationResume); $locationId = $con->insert_id; #$locationId = $con->execute(); echo $locationId; } }
谢谢您的帮助.
只有在执行查询后才能获取last_insert_id.