I want to execute this SQL query, but it does not display anything.

add_action('admin_menu', 'instafeed_setup_menu'); function instafeed_setup_menu(){ add_menu_page( 'Test Plugin Page', 'InstaFeed', 'manage_options', 'insta-feed', 'instafeed_admin_panel' ); } function instafeed_admin_panel(){ $fivesdrafts = $wpdb->get_results("SELECT * FROM options"); foreach ($fivesdrafts as $fivesdraft) { echo $fivesdraft->id; } } 

The code is in the main file of the plugin. Perhaps wordpress requests are executed differently. Tell me what is the error and how do I execute this query on wordpress?

  • 3
    The answer you gave is correct, but WP does not. There are functions for reading and updating options that are well cached. Read about get_option () wp-kama.ru/function/get_option and update_option () associated with it - KAGG Design
  • one
    Do you have an exact options table without a prefix in the database? - tutankhamun
  • one
    @tutankhamun most likely yes. See how it processes data from this table. Its code is pretty pointless for wp_options. - KAGG Design 5:46 pm
  • one
    @KAGGDesign Agree. Then it turns out the functions *_option() do not help the author of the question - tutankhamun
  • one
    @tutankhamun I suggested to him not to climb into the database with my own brake queries, throw out the options table altogether, and use the functions as expected - KAGG Design

1 answer 1

You have no $wpdb variable inside the instafeed_admin_panel() $wpdb , so nothing happens. Not an expert in WP but try adding a global $wpdb line global $wpdb this:

 function instafeed_admin_panel(){ global $wpdb; $fivesdrafts = $wpdb->get_results("SELECT * FROM options"); foreach ($fivesdrafts as $fivesdraft) { echo $fivesdraft->id; } } 
  • The answer is correct, the point is to declare a global variable. - KAGG Design