Title: PHP Warning: Missing argument 2 for wpdb::prepare()
Author: Andrew Nacin
Published: December 12, 2012
Last modified: July 10, 2015

---

 [  ](https://profiles.wordpress.org/nacin/) [Andrew Nacin](https://profiles.wordpress.org/nacin/)
6:14 am _on_ December 12, 2012     
Tags: [3.5 ( 27 )](https://make.wordpress.org/core/tag/3-5/),
[dev-notes ( 650 )](https://make.wordpress.org/core/tag/dev-notes/), sql, [wpdb ( 6 )](https://make.wordpress.org/core/tag/wpdb/)

# PHP Warning: Missing argument 2 for wpdb::prepare()

Hello pluginPlugin A plugin is a piece of software containing a group of functions
that can be added to a WordPress website. They can extend functionality or add new
features to your WordPress websites. WordPress plugins are written in the PHP programming
language and integrate seamlessly with WordPress. These can be free in the WordPress.
org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
or can be cost-based plugin from a third-party. or theme author! You possibly found
this post after searching the Internet for the error above: “PHPPHP The web scripting
language in which WordPress is primarily architected. WordPress requires PHP 7.4
or higher Warning: Missing argument 2 for wpdb::prepare().”

So, this is a new warning in 3.5. **No sites are broken,** everything is fine as
before. But, this is indeed something you need to look at, because you may be exposing
your users to a possible SQL injection vulnerability. Now that’s no fun!

First, if you’re a user and you want to get rid of these errors, you should turn
off the displaying of errors in PHP. There are many ways to do this, such as in 
php.ini, .htaccess, etc. For this, you can just put this in wp-config.php. (Note
that hiding errors on production sites is good practice anyway.)

    ```notranslate
    @ini_set('display_errors', 0);
    ```

If you’re a user, you can stop here. **(If you need more help, please don’t comment
here, try the helpful [Support Forums](https://wordpress.org/support/).)** Just 
be sure to send a link to this post to the developer of the theme or plugin referenced
in the error.

Now, developers: Here’s how $wpdb->prepare() is supposed to work:

    ```notranslate
    $wpdb->prepare( "SELECT * FROM table WHERE ID = %d AND name = %s", $id, $name );
    ```

See how $id — an integer, presumably — was passed as the second argument? That corresponds
to the first placeholder, %d. Then, $name (a string) was passed as the third argument,
thus the second placeholder, %s. This makes sure your query is safe, and prevents
something like [little bobby tables](http://xkcd.com/327/). (Note: the comic is 
wrong, don’t sanitize — always prepare your queries.)

The problem is, a number of people were calling $wpdb->prepare() with only one argument,
like so:

    ```notranslate
    $wpdb->prepare( "SELECT COUNT(*) FROM table" );
    ```

See, there’s no parameter (%d, %s, or for floats, %f) in this query. This happens
to work fine, but the prepare call isn’t doing anything. You should instead the 
query directly, as there are no inputs.

But here’s where the problem lies:

    ```notranslate
    $wpdb->prepare( "SELECT * FROM table WHERE id = $id" );
    ```

See the problem? That query isn’t secure! You may think you are “preparing” this
query, but you’re not — you’re passing $id directly into the query, unprepared. 
And **this**, right here, is why $wpdb->prepare() now issues a warning if it isn’t
called with more than one argument. Because you can’t prepare a query without more
than one argument. Here’s a correct example:

    ```notranslate
    $wpdb->prepare( "SELECT * FROM table WHERE id = %d", $id );
    ```

This wasn’t a decision done lightly. We don’t like shoving PHP warnings into the
faces of users and developers. But given the potential security risks, we wanted
everyone to immediately look at how they are running queries. And, of course, always
prepare them properly.

For more: [wpdb Codex reference](https://codex.wordpress.org/Class_Reference/wpdb),
[#22262](https://core.trac.wordpress.org/ticket/22262), and [[22429]](https://core.trac.wordpress.org/changeset/22429).

[#3-5](https://make.wordpress.org/core/tag/3-5/), [#dev-notes](https://make.wordpress.org/core/tag/dev-notes/),
[#sql](https://make.wordpress.org/core/tag/sql/), [#wpdb](https://make.wordpress.org/core/tag/wpdb/)

### Share this:

 * [  Threads ](https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/?share=bluesky)
 * [  X ](https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/?share=x)
 * [  Facebook ](https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/?share=facebook)
 * [  LinkedIn ](https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/?share=linkedin)

 1.  ![](https://secure.gravatar.com/avatar/250257111d1248c34046552bbcdf6999949ef56b2f992dd5eb39bf45cabeb18a?
     s=32&d=mm&r=g)
 2.   [Samuel Wood (Otto)](https://profiles.wordpress.org/otto42/)  6:17 am _on_ December
     12, 2012
 3.  Dangit. Stole my ottopress post for tomorrow. Now I have to come up with fresh,
     original content. 😉
 4.   * ![](https://secure.gravatar.com/avatar/c64b8f97639b146b3a504e7d2cb44a6d7296f3a607e767abe41fa2cbc50cadbc?
        s=32&d=mm&r=g)
      *  [Pippin (mordauk)](https://profiles.wordpress.org/mordauk/)  6:21 am _on_ 
        December 12, 2012
      * Damn, stole my Pippin’s Plugins post for tomorrow. Seriously 😀
      * ![](https://secure.gravatar.com/avatar/1a7df76f4f6519a5618c32428b277a5c7a89798394965b67e50528ca57c1b5a8?
        s=32&d=mm&r=g)
      *  [Andrew Nacin](https://profiles.wordpress.org/nacin/)  6:23 am _on_ December
        12, 2012
      * Find something else, suckers. 🙂 Lots of juicy stuff in 3.5! Also, make/plugins
        and make/coreCore Core is the set of software required to run WordPress. The
        Core Development Team builds WordPress. could both use content!
         - ![](https://secure.gravatar.com/avatar/27180c82524020e7339e2b30818b6d8ab004c54f730f12b1434b1e365be0e175?
           s=32&d=mm&r=g)
         -  [Ipstenu (Mika Epstein)](https://profiles.wordpress.org/ipstenu/)  6:47
           am _on_ December 12, 2012
         - And he updated the forums… Well hell, I’m having a drink and going to bed!
         - ![](https://secure.gravatar.com/avatar/32c5cc7746882995949359e61e2eb608a441551274a86242ecb9d04720bbf333?
           s=32&d=mm&r=g)
         -  [Justin Sternberg](https://profiles.wordpress.org/jtsternberg/)  3:33 pm
           _on_ December 12, 2012
         - We need comment “like” buttons.
         - ![](https://secure.gravatar.com/avatar/6ae7dc2f286ea46a3ade6742b977dc2aedf1cfa8e35470547b7bec7ddd27dc57?
           s=32&d=mm&r=g)
         -  [tmontg2](https://profiles.wordpress.org/tmontg2/)  5:49 pm _on_ December
           14, 2012
         - lol! too funny. Thanks for the post!
 5.  ![](https://secure.gravatar.com/avatar/213ffa27eb35ac4a16040157d8d9d8c9a1be19971a387774a8891893215ed084?
     s=32&d=mm&r=g)
 6.   [Emil Uzelac](https://profiles.wordpress.org/emiluzelac/)  6:23 am _on_ December
     12, 2012
 7.  Good one, just submitted this [https://wordpress.org/support/topic/warning-missing-argument-2-for-wpdbprepare-3](https://wordpress.org/support/topic/warning-missing-argument-2-for-wpdbprepare-3)
     15 minutes ago. Let me link to this post as well, to help author out 🙂
 8.  Thanks,
      Emil
 9.  ![](https://secure.gravatar.com/avatar/9e580daefb90cff3eb80f1c7ca61e73e7627b453593dffc3985dbdab3bf3507e?
     s=32&d=mm&r=g)
 10.  [chacha102](https://profiles.wordpress.org/chacha102/)  6:30 am _on_ December
     12, 2012
 11. > Because you can’t prepare a query with more than one argument. Here’s a correct
     > example:
 12. I think you mean, you can’t prepare a query without more than one argument.
 13.  * ![](https://secure.gravatar.com/avatar/1a7df76f4f6519a5618c32428b277a5c7a89798394965b67e50528ca57c1b5a8?
        s=32&d=mm&r=g)
      *  [Andrew Nacin](https://profiles.wordpress.org/nacin/)  6:31 am _on_ December
        12, 2012
      * Updated, thanks.
 14. ![](https://secure.gravatar.com/avatar/6330f7dce02643873a663642ee318da40f19bccc016f9247f0d3d40eeaf8d752?
     s=32&d=mm&r=g)
 15.  [Brian Layman](https://profiles.wordpress.org/brianlayman/)  6:56 am _on_ December
     12, 2012
 16. Interesting.. So the rule of “Always use prepare on queries” is simply wrong.
     
     It should be “Always use prepare on queries that built with variable arguments.”
 17. I’d always thought it did further sanitization of the query string itself, but
     I suppose that would be really hard to do without blocking some valid query people
     would inevitably want.
 18.  * ![](https://secure.gravatar.com/avatar/250257111d1248c34046552bbcdf6999949ef56b2f992dd5eb39bf45cabeb18a?
        s=32&d=mm&r=g)
      *  [Samuel Wood (Otto)](https://profiles.wordpress.org/otto42/)  6:58 am _on_
        December 12, 2012
      * The *vast* majority of problems with this I’ve seen today had variable arguments,
        but were putting them directly in the strings, like the third code example 
        there. Which basically means that prepare did nothing to protect them.
      * Previously, prepare(‘string’) returned ‘string’. Now it returns ‘string’ and
        a warning that you are doing-it-wrong. 🙂
      * ![](https://secure.gravatar.com/avatar/9e580daefb90cff3eb80f1c7ca61e73e7627b453593dffc3985dbdab3bf3507e?
        s=32&d=mm&r=g)
      *  [chacha102](https://profiles.wordpress.org/chacha102/)  7:03 am _on_ December
        12, 2012
      * If you check out the source code for wpdb::prepare it really isn’t that interesting.
 19. ![](https://secure.gravatar.com/avatar/72000f0cd0856c3f6bfeb37c4b311e64809aac881335b76d0a44952202927d9b?
     s=32&d=mm&r=g)
 20.  [Vitor Carvalho](https://profiles.wordpress.org/lightningspirit/)  11:17 am _on_
     December 12, 2012
 21. Fantastic explanation Nacin 😉
 22. ![](https://secure.gravatar.com/avatar/6b6d1c4d29f1b149da0008917c70b019408d5af404a46bca853e4dd33546e1ed?
     s=32&d=mm&r=g)
 23.  [Joost de Valk](https://profiles.wordpress.org/joostdevalk/)  2:14 pm _on_ December
     12, 2012
 24. Had to chuckle a bit when I found this:
 25. Missing argument 2 for wpdb::prepare(), called in /home/example/public_html/wp-
     content/plugins/akismet/admin.php
 26. ![](https://secure.gravatar.com/avatar/1700284eaa29cdbf923334d8709372972865dab829813c6cc0e84bef73da4ef8?
     s=32&d=mm&r=g)
 27.  [rfair404](https://profiles.wordpress.org/rfair404/)  2:14 pm _on_ December 12,
     2012
 28. Thanks Nacin, I started seeing these notices in the last few weeks on several 
     plugins that I use. glad to know what’s going on here.
 29. ![](https://secure.gravatar.com/avatar/809bcc5205828becb7f65586d9c3d2c5bd132b8ed57984eee48cad46216a9f9c?
     s=32&d=mm&r=g)
 30.  [a6april](https://profiles.wordpress.org/a6april/)  3:18 pm _on_ December 12,
     2012
 31. Thanks Huge Andrew, I had no idea! I am always glad to learn something everyday!
     I appreciate the quick followup and all of the responses. Have a great day all!
 32. ![](https://secure.gravatar.com/avatar/980e8a85d33c1327e80d3ef149b40c7f300bef579925b2f51b51b2ecd3c26671?
     s=32&d=mm&r=g)
 33.  [Josh](https://profiles.wordpress.org/josh401/)  4:31 pm _on_ December 12, 2012
 34. Extremely useful… and “juicy”! Thanks Andrew! Saved me tons of time. Nice to know
     you guys are on top of security. Thanks again!
 35. ![](https://secure.gravatar.com/avatar/4b208a58c45769c479d7ad9dab4f695eb7e3a5548314d7097b2397a26f665640?
     s=32&d=mm&r=g)
 36.  [nomadentech](https://profiles.wordpress.org/nomadentech/)  11:29 am _on_ December
     13, 2012
 37. This is works perfectly, i think similar error will found in another plugins, 
     so we dont need to fear for updating WordPress coreCore Core is the set of software
     required to run WordPress. The Core Development Team builds WordPress.. 😀
      Thank
     you,
 38. Teguh
 39. ![](https://secure.gravatar.com/avatar/439309f5debfaa2702647c2bb385ffa06fd40a7d13700d178b83057cd92b5235?
     s=32&d=mm&r=g)
 40.  [properwp](https://profiles.wordpress.org/properwp/)  6:01 pm _on_ December 13,
     2012
 41. Thank you very much! We’re using a modified version of a clunky pluginPlugin A
     plugin is a piece of software containing a group of functions that can be added
     to a WordPress website. They can extend functionality or add new features to your
     WordPress websites. WordPress plugins are written in the PHP programming language
     and integrate seamlessly with WordPress. These can be free in the WordPress.org
     Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
     or can be cost-based plugin from a third-party. in the repo and we keep finding
     new, wonderful problems. Another one of those “probably should have started from
     scratch” situations!
 42. ![](https://secure.gravatar.com/avatar/b47b1949f701eb85df19cb7f0af9776567aa9819fccd92c9a6d99ab41e6a721a?
     s=32&d=mm&r=g)
 43.  [Mark de Scande BlogLines](https://profiles.wordpress.org/bloglines/)  8:04 pm
     _on_ December 13, 2012
 44. The only thing here is that it should have been noted some were or it was and 
     i did not see it or i did not read i had the same problem on my Wife site SuperBlogs.
     co.za but on BlogLines.co.za it all was perfect on SB i just added some dirty 
     code to make it go away
 45. [https://wordpress.org/support/topic/err-after-upgrade-to-35?replies=9](https://wordpress.org/support/topic/err-after-upgrade-to-35?replies=9)
 46. But thx for posting it here for us all to see
 47. ![](https://secure.gravatar.com/avatar/87d1a16a387f40e98c0195a51d277d413bc80b04453954f01bd477d697435257?
     s=32&d=mm&r=g)
 48.  [Nashwan Doaqan](https://profiles.wordpress.org/alex-ye/)  5:32 pm _on_ December
     14, 2012
 49. Thank You Andrew Nacin , I see many plugins have this PHPPHP The web scripting
     language in which WordPress is primarily architected. WordPress requires PHP 7.4
     or higher warning .
      I hope a good life for all plugins authors 😀
 50. ![](https://secure.gravatar.com/avatar/7a33f224abe847cf3a948901339724083e07d43f3584f87b150a7cae46e2aaa8?
     s=32&d=mm&r=g)
 51.  [DigiproveDevelopment](https://profiles.wordpress.org/digiprovedevelopment/)  
     12:51 pm _on_ December 15, 2012
 52. Correction: some sites WERE broken because the warning messages screwed up the
     buffer and prevented normal operation.
 53. Am I the only one who thinks it was the wrong decision for WordPress team to decide
     that warnings will be spouted out for a situation where previously there was not
     even a Notice-level message? Like many authors I turn on notice-level messages
     when testing. And the underlying change caused so much difficulty because of a
     widespread but incorrect assumption that wpdb->prepare did actually do something
     with standalone sql strings when it fact what it did was – nothing.
 54. Suggest that:
      a) WordPress automatic upgrade process at least gives a warning
     to users who are using plugins not marked as being compatible with new release(
     e.g. like Firefox does).
 55. b) More use is made of notice-level messages fo situations like this so that sites
     don’t actually break when WordPress changes
 56. c) All registered pluginPlugin A plugin is a piece of software containing a group
     of functions that can be added to a WordPress website. They can extend functionality
     or add new features to your WordPress websites. WordPress plugins are written 
     in the PHP programming language and integrate seamlessly with WordPress. These
     can be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
     or can be cost-based plugin from a third-party. developers are warned by email
     of situations identified in betaBeta A pre-release of software that is given out
     to a large group of users to trial under real conditions. Beta versions have gone
     through alpha testing in-house and are generally fairly close in look, feel and
     function to the final product; however, design changes often occur as part of 
     the process. testing which require action

Comments are closed.

# Post navigation

[← WordPress 3.5 admin columns for custom taxonomies](https://make.wordpress.org/core/2012/12/11/wordpress-3-5-admin-columns-for-custom-taxonomies/)

[Post-3.5 IRC meeting →](https://make.wordpress.org/core/2012/12/12/post-3-5-irc-meeting/)