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 badges1 Answer
Reset to default 2You can get the last inserted ID from $wpdb
with the insert_id
property:
$wpdb->insert( ... );
$couponId = $wpdb->insert_id