Get data from database of WordPress in Plugin

I am trying to get last inserted data from WP database but it shows nothing.

code :

Read More
global $wpdb;
    $lastid = $wpdb->insert_id;
    $table = $wpdb->prefix."videos";
    $result = $wpdb->get_results("SELECT * FROM $table WHERE id = '$lastid'");
     foreach ( $result as $print )   {

  echo '<tr>';
  echo '<td>' . $print->video.'</td>';

  echo '</tr>';
}

Please advise.

Related posts

1 comment

  1. From the Codex (emphasis mine):

    After insert, the ID generated for the AUTO_INCREMENT column can be
    accessed with:

    $wpdb->insert_id

    It only makes sense to use $wpdb->insert_id after an insert statement. Otherwise it won’t be set.

    Depending on your table and your requirements, something like:

    $result = $wpdb->get_results("SELECT * FROM $table WHERE id = (select max(id) from $table)");
    

    might do what you need.

Comments are closed.