Muhammad Yousuf
Muhammad Yousuf

Reputation: 19

Get data from database of Wordpress in Plugin

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

code :

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.

Upvotes: 1

Views: 4954

Answers (1)

Hobo
Hobo

Reputation: 7611

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.

Upvotes: 2

Related Questions