如何使用php(ODBC)获取最后一条记录插入ID?

时间:2015-11-10 16:02:27

标签: php sql-server

我认为我在这里过于复杂,我试图传递插入记录的ID。

我已经设法使用以下代码使用MYSQL:

$queryadd = "INSERT INTO events (event_date,event_orig_name,event_orig_tel,event_orig_email,event_title,event_time_lunch,event_room,event_cater,event_type,event_attendees,event_diary,event_user_email,event_user_tel,event_status,event_user_id) VALUES ('$event_date','$event_orig_name','$event_orig_tel','$event_orig_email','$event_title','$event_time_lunch','$event_room','$event_cater','$event_type','$event_attendees','$event_dieray','$event_user_email','$event_user_tel','$event_status','$event_user_id')";

$resultadd = $conn->query($queryadd);
$ideventadd = $conn->insert_id;
if(!$resultadd) die($conn->error);

那么,在ODBC服务器上使用它的替代方法是什么,我的代码如下:

$queryadd = "INSERT INTO events (event_date,event_orig_name,event_orig_tel,event_orig_email,event_title,event_time_lunch,event_room,event_cater,event_type,event_attendees,event_diary,event_user_email,event_user_tel,event_status,event_user_id) VALUES ('$event_date','$event_orig_name','$event_orig_tel','$event_orig_email','$event_title','$event_time_lunch','$event_room','$event_cater','$event_type','$event_attendees','$event_dieray','$event_user_email','$event_user_tel','$event_status','$event_user_id')";

$resultadd = odbc_exec($connect, $queryadd);
if(!$resultadd) die($connect->error);

$queryaddid = "SELECT @@identity AS evtid";
$resultaddid = odbc_exec($connect, $queryaddid);
$rowaddid = odbc_fetch_array($resultaddid);
$ideventadd = $rowaddid['event_id'];
if(!$resultaddid) die($connect->error);

0 个答案:

没有答案