How to Solve PHP Warning: Missing Argument 2 for WPDB Prepare
Today, I helped my client to upgrade his wordpress-based website to the latest version 3.5, after updating, I got the error message as shown as below:
PHP Warning: Missing argument 2 for wpdb::prepare()
Although the error was showed on, the website was still working good as before, but the problem could not be ignored.
Now, the Missing argument 2 for wpdb prepare problem has been solved, the ways to solve the Missing argument 2 for wpdb prepare problem are simple, unless you installed many plugins on your WordPress website, It may be difficult for beginners to modify the PHP source code of the plugin which uses the $wpdb->prepare() function.
Missing Argument 2 for Wpdb Prepare, Why?
WordPress team made a change to $wpdb->prepare() usage to prevent possible SQL injection vulnerability in 3.5, so this error only happens on the WordPress 3.5 websites.
Ways to solve Missing Argument 2 for wpdb prepare
- Update the problem plugins.
- Hide error message. (not recommended)
- Modify the source - hard, but correct.
Way (1) Update the problem plugins
You need to update the plugins to the latest. How to find the problem plugins? You can just find the plugin paths which are displayed in the error message.
Warning: Missing argument 2 for wpdb::prepare(), called in /home/example001/public_html/wordpress/wp-content/plugins/membership/membershipincludes/classes/class.adminbar.php on line 77
In this error message, you can find out the problem plugin's path is called "membership", and then, login to your WordPress admin control panel, click Plugins, the plugin list will be showed, and then think about what installed plugin is related to "membership".
A good plugin author will update their plugins because of issues, so you can check out if any update is available for the plugin.
Way (2) Hide error message
It is not recommended but if you want to do it, the PHP warning messages will be hided after adding this code into any line of your wp-config.php.
Way (3) Modify the source
If you're an expert in programming PHP, modifying the source may be easy for you, and you can solve the Missing argument 2 for wpdb::prepare() problem by yourself.
Check out the output error message and identify the source of the error (which file, which line), and then modify the line for new $wpdb->prepare() usage.
For example, for the previous version (before 3.5) of $wpdb->prepare() usage, the code looks like this.
$wpdb->prepare( "SELECT * FROM table WHERE id = $id AND name = $name" );
In 3.5, you need to add the two arguments to pass the $id and $name into the query.
$wpdb->prepare( "SELECT * FROM table WHERE id = %d AND name = %s", $id, $name );
Use a text editor software such as Notepad++ to modify the PHP files, it shows the line number to help you find the code quickly.
Only useful comments will be allowed.