admin管理员组

文章数量:1022784

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

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

本文标签: databaseTrouble running wpdbgtquery() with lastinsertid