Php mysql_insert_id()的等价物是什么;使用事先准备好的声明?

Php mysql_insert_id()的等价物是什么;使用事先准备好的声明?,php,sql,pdo,Php,Sql,Pdo,我已使用prepared语句将订单插入orders表,但现在我想根据orders表中插入的最后一个ID将订单项插入order items表: if (isset($_POST['process'])) { $order_name = $_POST['order_name']; //create initial order $stmt = $conn2->prepare("INSERT INTO orders (order_name) VALUES (?)");

我已使用prepared语句将订单插入orders表,但现在我想根据orders表中插入的最后一个ID将订单项插入order items表:

if (isset($_POST['process'])) {

    $order_name = $_POST['order_name'];
    //create initial order
    $stmt = $conn2->prepare("INSERT INTO orders (order_name) VALUES (?)");
    $stmt->bind_param('s', $order_name);
    $stmt->execute();

    //this gets the most recent auto incremented ID from the database - this is the order_id we have just created
    $order_id = mysql_insert_id();

    //loop over all of our order items and add to the database
    foreach ($_SESSION['order'] as $item) {
什么是mysql_insert_id()的等价物使用准备好的语句?

如果您使用的是PDO,则它是。因此,如果您的数据库句柄被称为
$link

$order_id = $link->lastInsertId();
如果您使用的是MySQLi,则它是或
MySQLi\u insert\u id()


您可以尝试
$stmt->insert_id
获取插入的id

什么是mysql_insert_id();在准备好的语句风格中?你的意思是,PDO库中的
mysql\u insert\u id
的等价物是什么?有什么问题吗?但我在mysqli中使用的是预先准备好的语句,而不是PDO@user1227124:那么它就是或
mysqli\u insert\u id()
@user1227124:下次,请在文档中查找它。我们期待着对这些问题进行事先研究。
$order_id = $link->insert_id;