WordPress.org

Ready to get started?Download WordPress

Forums

Select Featured Posts
Error with WP 3.5: wpdb::prepare() (61 posts)

  1. purrdesign
    Member
    Posted 1 year ago #

    Seems like this is a pretty common error with the new wp 3.5 release, but getting a "Missing argument 2 for wpdb::prepare() on line 75" error in the Posts page for the plugin after upgrade. It still seems to function ok for already selected posts on the front-end, but the error prevents selecting of any new posts.

    Assuming we wont be getting an upgrade since it hasn't been updated in over 2 years but though someone here might have a fix?

    http://wordpress.org/extend/plugins/select-featured-posts/

  2. This is due to wpdb::prepare() requiring a minimum of 2 arguments as of 3.5. Plugin/theme authors should be referred to this changeset: https://core.trac.wordpress.org/changeset/22429

  3. Andrew Nacin
    Lead Developer
    Posted 1 year ago #

    If you're a user: Your site is fine. If you're a plugin/theme author, please read: http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/. If you're a user, please point your plugin/theme author to that post.

  4. purrdesign
    Member
    Posted 1 year ago #

    I'm sorry but I'm not a developer and am having a hard time applying these fixes to this plugin. Looking at the referenced lines in the error this is what is currently there:

    <?php
          global $wpdb, $post;
          $checked = '';
          if ($post->post_status == 'publish' && $post->post_password == '' && $post->post_type == 'post') {
            $res = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->featured_posts WHERE post_id = $post_id"));
            if ($res) {
              $checked = 'checked="true"';
            } else {
              $checked = '';
            } ?>

    I see the wpdb->prepare argument but not sure what I need to change it to.

  5. Replace this:

    $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->featured_posts WHERE post_id = $post_id"));

    With this:

    $wpdb->get_var($wpdb->prepare( "SELECT post_id FROM $wpdb->featured_posts WHERE post_id = %d", $post_id ));
  6. purrdesign
    Member
    Posted 1 year ago #

    That worked, but now I'm getting a new error on a new line. Code appears to be:

    if ($action_delete == 1) {
          $success = $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->featured_posts WHERE post_id = $post_id "));
        } else {
          $success = $wpdb->insert($wpdb->featured_posts, array( 'post_id' => $post_id, 'created' => time() ), array( '%d', '%d' ) );
        }

    I can change the one line to:
    $success = $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->featured_posts WHERE post_id = $d ", $post_id ));

    That seems to work, but I get an error of "There was some error while updating.Please try again" when unchecking featured posts, so I assume some change needs to be made to the other wpdb line.

  7. Jacob Schweitzer
    Member
    Posted 1 year ago #

    purrdesign -
    It should be like this I believe:

    $success = $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->featured_posts WHERE post_id = %d ",$post_id));

    The $d needs to be a %d in order for the variable to be replaced just like Drew did otherwise your code looks correct.

  8. MAzZY
    Member
    Posted 1 year ago #

    How can I change this?

    $postids = $wpdb->get_col( $wpdb->prepare(
                "
                SELECT ID FROM $wpdb->posts
                    WHERE post_status = 'publish'
                        AND post_type = 'post'
                "
            ) );
  9. Markn1223
    Member
    Posted 1 year ago #

    Anyone can help me edit this line,please.
    $tax_array = $wpdb->get_results($wpdb->prepare($sql));

  10. Ed
    Member
    Posted 1 year ago #

    Can someone help with this line, generating the same error:

    $th_vers = $wpdb->get_var( $wpdb->prepare( "SELECT th_vers FROM $table WHERE id = 1 LIMIT 0,1" ) );

  11. AlanCWebb
    Member
    Posted 1 year ago #

    Hey everyone, I'm having the same problem...

    The Warning:
    "Warning: Missing argument 2 for wpdb::prepare(), called in /home/hubdc/hubdconline.org/wp-content/themes/brainstorm/functions/sidebars.php on line 70 and defined in /home/hubdc/hubdconline.org/wp-includes/wp-db.php on line 990"

    The Referenced line of code
    $widgetized_pages = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'jw_sidebar'"));

    I'm also not a developer... any ideas??

  12. AlexRayan
    Member
    Posted 1 year ago #

    Hi everyone!

    In case some people are still wondering how to change the code to properly pass 2 arguments to prepare(), here is an example:

    //OLD:
    $wpdb->prepare( "SELECT * FROM some_table WHERE ID = $id AND name = $name" );
    //NEW:
    $wpdb->prepare( "SELECT * FROM some_table WHERE ID = %d AND name = %s", $id, $name );

    %d for integers, %s for strings, %f for floats.

    The line of code posted by AlanCWebb would be changed the following way:

    //OLD
    $widgetized_pages = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'jw_sidebar'"));
    //NEW
    $jw_sidebar = 'jw_sidebar';
    $widgetized_pages = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s",$jw_sidebar));

    Hope, it helps someone.

    Regards,
    Alex

  13. AlanCWebb
    Member
    Posted 1 year ago #

    @AlexRayan, I owe you a beer. Thanks so much!

  14. shadowspid
    Member
    Posted 1 year ago #

    need help here for a plugin that has no more support from author, gurus please assist..

    $sql = $this->db->prepare( "SELECT * FROM {$this->popover} WHERE popover_active = 1 ORDER BY popover_order ASC" );

  15. shadowspid
    Member
    Posted 1 year ago #

    and.. one more syntax error..

    <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Footer Column 1')){};?>

    pls help... thanks

  16. AlexRayan
    Member
    Posted 1 year ago #

    Hi Shadowspid!

    Regarding the first piece of code, you would need to move the property out of the query and into a variable before it, then pass the variable as a second argument to the function:

    $popover = $this->popover;
    $sql = $this->db->prepare( "SELECT * FROM %s WHERE popover_active = 1 ORDER BY popover_order ASC", $popover );

    Regarding the second piece of code, it's unclear what the problem is. What errors are you getting with it?

    Regards,
    Alex

  17. shadowspid
    Member
    Posted 1 year ago #

    Hi Alex here's the error i got at the footer of the site:
    Parse error: syntax error, unexpected ';' in /home/bridgest/public_html/wp-content/themes/itworx_1.3/footer.php on line 35

  18. AlexRayan
    Member
    Posted 1 year ago #

    Hi Shadowspid,

    Sorry, I missed the error in the code when looking at it last time. It does have an unexpected ";" as the error states. Remove ";" from after

    if (function_exists('dynamic_sidebar') && dynamic_sidebar('Footer Column 1')){
    }

    It's not needed there.

    Regards,
    Alex

  19. shadowspid
    Member
    Posted 1 year ago #

    many thanks alex.. i found out that the file was hacked.

  20. glitterdust_ZA
    Member
    Posted 1 year ago #

    Hi there

    I'm experiencing a similar problem :(

    Stuck with an error("Missing argument 2 for wpdb::prepare()...") for the following code:

    function nws_get_theme_version() {
    global $wpdb;

    $th_vers = NULL;

    // the table
    $table = is_dbtable_there('status');
    // the sql
    $th_vers = $wpdb->get_var( $wpdb->prepare( "SELECT th_vers FROM $table WHERE id = 1 LIMIT 0,1" ) );

    return $th_vers;
    }

    Would appreciate it if anybody could help!

  21. vadim_k
    Member
    Posted 1 year ago #

    Hello! Help me please! I'm experiencing a similar problem :(

    This lines with error (Warning: Missing argument 2 for wpdb::prepare() in /home/u119588452/public_html/wp-includes/wp-db.php on line 990):

    foreach ($files as $key=>$file) {
    $ar_file_id[] = $file->ID;
    $ar_link[$file->ID] = & $files[$key];
    }

    and:

    usort ($files,'_sort_order');
    foreach ($files as $ord=>$row) {
    if($row->menu_order == 0)
    $ar_link[$row->post_parent]->files[] = $row;
    else
    $ar_link[$row->post_parent]->files[$row->menu_order] = $row;
    }

    Plugin name - Ads_Wordpress.

    Help me? please!!!!:'(

  22. AlexRayan
    Member
    Posted 1 year ago #

    Hello vadim_k!

    Could you please post the line of code in question with prepare() function used? The lines of code that you posted don't have that function. It's hard to guess what errors they are outputting without errors specified.

    Regards,
    Alex

  23. vadim_k
    Member
    Posted 1 year ago #

    Alex, this?

    Warning: Missing argument 2 for wpdb::prepare(), called in /home/u119588452/public_html/wp-content/plugins/ads-wordpress/ads_admin.php on line 23 and defined in /home/u119588452/public_html/wp-includes/wp-db.php on line 990

  24. AlexRayan
    Member
    Posted 1 year ago #

    Hi Vadim,

    I was trying to look for the plugin online to download it and see the code but I couldn't find it.
    Please copy paste the code that is on line 23 in ads_admin.php in the plugin folder (ads-wordpress) and I'll help you correct it. This is the line of code that needs to be changed since it's using prepare() incorrectly (that's what the error you posted tells us).

    Best regards,
    Alex

  25. vadim_k
    Member
    Posted 1 year ago #

    Hi, Alex!

    here the code that is on line 23:

    $ads_user_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_login = '$ads_user_name'" ));

    But now is showing error with this text:
    Warning: Missing argument 2 for wpdb::prepare() in /home/u119588452/public_html/wp-includes/wp-db.php on line 990.

    p.s.: my English is bad, sorry, please...

  26. vadim_k
    Member
    Posted 1 year ago #

    Alex, link for download this plugin
    http://wp.od.ua/files/ads-wordpress_4.zip

  27. AlexRayan
    Member
    Posted 1 year ago #

    Hi Vadim,

    Here's what you have to change in ads_admin.php on line 23:

    From:
    $ads_user_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_login = '$ads_user_name'" ));

    To:
    $ads_user_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_login = '%s'", $ads_user_name ));

    Best regards,
    Alex

  28. jlboelen
    Member
    Posted 1 year ago #

    I am having the same error with the following lines:

    $where = $wpdb->prepare("WHERE p.post_date < '".$current_post_date."' AND p.post_type = '". $post->post_type ."' AND p.post_status = 'publish' AND p.ID != '". $post->ID ."' $posts_in_ex_cats_sql");

    Is there anyone that could help me out? I am sorry, but I am not sure how to amend this myself :(

  29. Kapil Chugh
    Member
    Plugin Contributor

    Posted 1 year ago #

    Now you no longer need this plugin. You can simly use :

    http://wordpress.org/extend/plugins/my-posts-order/

  30. adrianhuma
    Member
    Posted 1 year ago #

    Hello guys. I have the same problem with a file....

    The line is :
    $has_children = $wpdb->get_var($wpdb->prepare("SELECT COUNT(meta_id) FROM $wpdb->postmeta WHERE meta_key='_menu_item_menu_item_parent' AND meta_value='".$item->ID."'"));

    Anyone ? A solution ?
    thanks a lot

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.