最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - Trouble running $wpdb->query() with last_insert_id

programmeradmin1浏览0评论

I am trying to get a primary key from a coupon table to put it in my target table as a foreign key. In between I would like to run a query to get the last insert id. I can't get the syntax right though

$couponId = $wpdb->query("select last_insert_id() from frequentVisitorCoupons_coupons");
  var_dump($couponId); // false
$couponId = $wpdb->query("select * from frequentVisitorCoupons_coupons where last_insert_id()");
  var_dump($couponId); // false

What is the correct syntax for this query?

I am trying to get a primary key from a coupon table to put it in my target table as a foreign key. In between I would like to run a query to get the last insert id. I can't get the syntax right though

$couponId = $wpdb->query("select last_insert_id() from frequentVisitorCoupons_coupons");
  var_dump($couponId); // false
$couponId = $wpdb->query("select * from frequentVisitorCoupons_coupons where last_insert_id()");
  var_dump($couponId); // false

What is the correct syntax for this query?

Share Improve this question asked May 1, 2019 at 3:07 Sean DSean D 3878 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can get the last inserted ID from $wpdb with the insert_id property:

$wpdb->insert( ... );
$couponId = $wpdb->insert_id
发布评论

评论列表(0)

  1. 暂无评论