在一个查询中获取当前插入ID

时间:2018-02-20 20:57:42

标签: php sql pdo

在我的查询中,我需要将我的行的自动增量ID插入另一列。

我需要protonumber列一起有3个字符串,但我无法弄清楚如何检索当前的lastinsertID并让它工作。

有什么想法吗?

        $stmt = $db->prepare('INSERT INTO protonumbers (season,program,designer,brand,date,protonumber) VALUES (:season, :program, :designer, :brand, :date, :protonumber)');
        $last_id = $pdo->lastInsertId();
        $stmt->execute(array(
            ':season' => $season,
            ':program' => $program,
            ':designer' => $designer,
            ':brand' => $brand,
            ':date' => $date,
            ':protonumber' => "{$program}-{$season}-{$last_id}"
            ));
        $id = $db->lastInsertId('protoID');

1 个答案:

答案 0 :(得分:-2)

使用SQL查询中的LAST_INSERT_ID()

或者

您也可以使用mysql_insert_id()来使用PHP获取它。

相关问题