相当于mysql_insert_id();使用准备好的语句? [英] What is equivalent of mysql_insert_id(); using prepared statement?

查看:66
本文介绍了相当于mysql_insert_id();使用准备好的语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Prepared语句将订单插入到订单表中,但是现在我想根据订单表中插入的最后一个ID将订单项插入到订单表中:

I have used prepared statement to insert an order into an orders table but now I want to insert the order items into the order items table based on the last ID inserted in the orders table:

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();的情况是什么?

What is equivalent to mysql_insert_id(); using prepared statement?

推荐答案

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

If you're using PDO, it's PDO::lastInsertId(). So if your database handle is called $link:

$order_id = $link->lastInsertId();

如果您使用的是MySQLi,则它是 mysqli::$insert_id mysqli_insert_id():

If you're using MySQLi, it's mysqli::$insert_id or mysqli_insert_id():

$order_id = $link->insert_id;

这篇关于相当于mysql_insert_id();使用准备好的语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆