2

How to get last updated row or ID in wordpress.

Like : $wpdb->insert_id;

9
  • you have to use it after $wpdb->insert() is executed. Have a look here stackoverflow.com/questions/1576018/… Commented Nov 14, 2017 at 4:27
  • I get last insert id.. but how I get last updated id...
    – SNS
    Commented Nov 14, 2017 at 4:30
  • In codeigniter $this->db->affected_rows that type in wordpress get the last affected_rows. It passable?.
    – SNS
    Commented Nov 14, 2017 at 4:36
  • i don't think there's a default function for it, what are you trying to update? Commented Nov 14, 2017 at 4:38
  • ok.. I get the last affected id and insert in the another table.. that only I ask last affected id getting is passable?.
    – SNS
    Commented Nov 14, 2017 at 4:41

2 Answers 2

1

I had the same problem.

There is no equivalent function. $wpdb->insert_id works only after $wpdb->insert

Alternative way

If it's an update, you can either get the id with a query based on the data you have got, or most of the time, you should have already got the id before update it.

global $wpdb;

$data = array(
    'first_name' => "John",
    'last_name' => "Doe"
);

$where = array(
    'id' => $my_id
);

$res_update = $wpdb->update( $wpdb->prefix . "my_table", $data, $where );

if( $res_update === false ){
     error_log( 'my error');
}

return $my_id;
0

Once again select the same row updated on success.

 if($wpdb->last_error !== '') {
      //throw error stuffs
    }else{
     $last_updated_row = $wpdb->get_row("SELECT * FROM $wpdb->prefix . 'my_table' WHERE id = $my_id");  

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.