Writing a wordpress plugin, trying to retrieve data from customized table?

I am trying to create a wordpress plugin. I had created a table and trying to retrieve data from the table but it give me an error message

Warning: Missing argument 2 for wpdb::prepare(), called in
/home2/l2on708/public_html/mysite.com/wp-content/plugins/myplugin/myplugin.php
on line 79 and defined in
/home2/l2on708/public_html/mysite.com/wp-includes/wp-db.php on line
1210

<?php
$table_name = $wpdb->prefix . "gallery_rating"; 


function options_page(){
    /*
    *  displaying back-end plugin page 
    */
    global $table_name;
    global $wpdb;


    $stmt = $wpdb->prepare("SELECT * FROM $table_name ORDER BY id DESC LIMIT 1 ");  
    $stmt->execute();
    $rows = $stm->fetchALL(PDO::FETCH_ASSOC);
    foreach ($rows as $rows) {

      $options_link = $rows['link'];
   }

   }


function table_install () {
      global $wpdb;
      global $table_name;


      $charset_collate = $wpdb->get_charset_collate();

      $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        link varchar(100) NOT NULL,
        src varchar(250) NOT NULL,
        click int(11) NOT NULL,
        UNIQUE KEY id (id)
      ) $charset_collate;";

      require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
      dbDelta( $sql );
   }   
   // run the install scripts upon plugin activation
   register_activation_hook(__FILE__,'table_install');
?>

Related posts

1 comment

  1. As of WordPress 3.5, wpdb::prepare() enforces a minimum of 2 arguments (as the error tells you):

    1. The query
    2. A value to substitute into the placeholder.

    More info can be found in the Codex.

    That said, prepare() may not even be the correct method for you to be using in this case; you may be better off using something like get_results().

Comments are closed.