执行多个查询时查找上次插入的id

时间:2017-04-27 10:21:20

标签: php mysql

我正在使用mysqli_multi_query执行多个查询。我尝试了很多东西,但无法找到最后插入的ID。

$fetchbasic = mysqli_fetch_array($resCheckres);
$cpyname = $fetchbasic['name'].'(copy)';

$query1 = "CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM name_webinfo WHERE resume_id = ".$resume_id.";
    UPDATE tmptable_1 SET resume_id = NULL , name = '".$cpyname."';
    INSERT INTO name_webinfo SELECT * FROM tmptable_1;
    DROP TEMPORARY TABLE IF EXISTS tmptable_1;";

$resCheckres = mysqli_multi_query($link,$query1);

我需要此查询的最后插入ID:INSERT INTO name_webinfo SELECT * FROM tmptable_1;

2 个答案:

答案 0 :(得分:0)

试试这个(在插入语句后放入select last_insert_id):

$fetchbasic = mysqli_fetch_array($resCheckres);
$cpyname = $fetchbasic['name'].'(copy)';

    $query1 = "CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM name_webinfo WHERE resume_id = ".$resume_id.";
        UPDATE tmptable_1 SET resume_id = NULL , name = '".$cpyname."';
        INSERT INTO name_webinfo SELECT * FROM tmptable_1;
        SELECT LAST_INSERT_ID();
        DROP TEMPORARY TABLE IF EXISTS tmptable_1;";


    $resCheckres = mysqli_multi_query($link,$query1);
    do {
        if ($result = $link->store_result()) {
           while ($row = $result->fetch_row()) {
           echo ($row);
           }
        }
    } while ($link->next_result());

答案 1 :(得分:0)

下面的代码对我有用,它会为你提供id数组

do {
    $ids[] = $link->insert_id;
     $link->next_result();
 } while($link->more_results());
 print_r(ids);