在WordPress更新到3.5+后,我在插件wpdb::Prepare()中收到许多错误

时间:2013-10-09 作者:need-help

在wordpress更新到3.5以上版本后,我在这个插件中发现了许多错误。我发现原因是:http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/

我不知道如何修理它。如果有人能帮我解决这些问题,或者给我一个如何解决的例子。

这是插件核心文件http://pastebin.com/Tk5Gf1HR

WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘wp_jsrm_menus’ at line 1 for query SHOW TABLES LIKE wp_jsrm_menus made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate
WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘wp_jsrm_items’ at line 1 for query SHOW TABLES LIKE wp_jsrm_items made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 169 and defined in /wp-includes/wp-db.php on line 992
PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, integer given in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 170
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 173 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘menuorder’ for query ALTER TABLE wp_jsrm_menus ADD menuorder mediumint(9) NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 176 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘label’ for query ALTER TABLE wp_jsrm_menus ADD label tinytext NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 179 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘itemheader’ for query ALTER TABLE wp_jsrm_menus ADD itemheader tinytext NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 182 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘valueheader’ for query ALTER TABLE wp_jsrm_menus ADD valueheader tinytext NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 189 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘valueheader2′ for query ALTER TABLE wp_jsrm_menus ADD valueheader2 tinytext NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 193 and defined in /wp-includes/wp-db.php on line 992
HP Warning: mysql_fetch_array() expects parameter 1 to be resource, integer given in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 194
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 197 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘image’ for query ALTER TABLE wp_jsrm_items ADD image tinytext made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 200 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘linked’ for query ALTER TABLE wp_jsrm_items ADD linked tinyint(1) NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 203 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘linkurl’ for query ALTER TABLE wp_jsrm_items ADD linkurl tinytext made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 206 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘itemhidden’ for query ALTER TABLE wp_jsrm_items ADD itemhidden tinyint(1) NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables
PHP Notice: Undefined variable: vh in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 212
PHP Warning: Missing argument 2 for wpdb::prepare(), called in /wp-content/plugins/simple-retail-menus/simple-retail-menus.php on line 213 and defined in /wp-includes/wp-db.php on line 992
WordPress database error Duplicate column name ‘value2′ for query ALTER TABLE wp_jsrm_items ADD value2 tinytext NOT NULL made by activate_plugin, do_action(‘activate_simple-retail-menus/simple-retail-menus.php’), call_user_func_array, jsrm_activate_loop, jsrm_activate, jsrm_update_db_tables

the errors comes from this code: i tried to fix as Charles Clarkson suggested but seems not helping.

[11-Oct-2013 05:27:09 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_menus\' at line 1 for query SHOW TABLES LIKE wp_jsrm_menus made by activate_plugin, do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate
[11-Oct-2013 05:27:09 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_items\' at line 1 for query SHOW TABLES LIKE wp_jsrm_items made by activate_plugin, do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate
[11-Oct-2013 05:27:11 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_menus\' at line 1 for query SHOW TABLES LIKE wp_jsrm_menus made by do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate
[11-Oct-2013 05:27:11 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_items\' at line 1 for query SHOW TABLES LIKE wp_jsrm_items made by do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate
[11-Oct-2013 05:27:11 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_menus\' at line 1 for query SHOW TABLES LIKE wp_jsrm_menus made by do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate
[11-Oct-2013 05:27:11 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_items\' at line 1 for query SHOW TABLES LIKE wp_jsrm_items made by do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate
[11-Oct-2013 05:30:45 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_menus\' at line 1 for query SHOW TABLES LIKE wp_jsrm_menus made by activate_plugin, do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate
[11-Oct-2013 05:30:45 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'wp_jsrm_items\' at line 1 for query SHOW TABLES LIKE wp_jsrm_items made by activate_plugin, do_action(\'activate_simple-retail-menus/simple-retail-menus.php\'), call_user_func_array, jsrm_activate_loop, jsrm_activate

1 个回复
最合适的回答,由SO网友:Charles Clarkson 整理而成

prepare() 至少需要2个参数。您的大多数电话prepare() 不需要。

例如:

$blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
在此代码中,如果调用$wpdb->prepare() 如果是有效的,它不会做任何事情。该函数对第一个参数没有任何作用。它处理传递的其他参数中的值,然后将这些参数插入到第一个参数中。

重写为:

$blogids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
在所有单个参数之后prepare() 通话已删除您应该具有以下功能:

// Update tables with new columns used in this version
$checkmenus = $wpdb->query( $wpdb->prepare( "SELECT * FROM %s LIMIT 1," $jsrm_menu_table ) );
$menucols = mysql_fetch_array($checkmenus);

if ( ! isset( $menucols[\'menuorder\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_menu_table ADD menuorder mediumint(9) NOT NULL" );

if ( ! isset( $menucols[\'label\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_menu_table ADD label tinytext NOT NULL" );

if ( ! isset( $menucols[\'itemheader\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_menu_table ADD itemheader tinytext NOT NULL" );

if ( ! isset($menucols[\'valueheader\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_menu_table ADD valueheader tinytext NOT NULL" );

// Loop to update the menu table value headers columns according to number of values.
for ( $v = 2; $v <= JSRM_VALUE_COLS; $v++ ) {
    $valh = \'valueheader\' . $v;
    if ( ! isset( $menucols[$valh] ) )
        $wpdb->query( "ALTER TABLE $jsrm_menu_table ADD $valh tinytext NOT NULL" );
}

$checkitems = $wpdb->query( "SELECT * FROM $jsrm_item_table LIMIT 1" );
$itemscols = mysql_fetch_array($checkitems);

if ( ! isset( $itemscols[\'image\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_item_table ADD image tinytext" );

if ( ! isset( $itemscols[\'linked\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_item_table ADD linked tinyint(1) NOT NULL" );

if ( ! isset( $itemscols[\'linkurl\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_item_table ADD linkurl tinytext" );

if ( ! isset( $itemscols[\'itemhidden\'] ) )
    $wpdb->query( "ALTER TABLE $jsrm_item_table ADD itemhidden tinyint(1) NOT NULL" );

// Loop to update the menu items value columns according to number of values.
for ( $v = 2; $v <= JSRM_VALUE_COLS; $v++ ) {
    $val = \'value\' . $v;
    if ( ! isset($menucols[$vh] ) )
        $wpdb->query( "ALTER TABLE $jsrm_item_table ADD $val tinytext NOT NULL" );
};

update_option( \'jsrm_db_version\', JSRM_DB_VERSION );
我还更改了条件式样式,并添加了一致的空白,以使代码更易于人类阅读。

结束

相关推荐

使用$wpdb查询新创建的表

我在wordpress数据库中创建了一个名为forms, 此表包括ID 和names我想使用wordpress查询此表中的所有列表$wpdb下面是我的代码<?php global $wpdb; $result = $wpdb->get_results ( \"SELECT * FROM $wpdb->forms\" ); foreach ( $result as $print ) { echo $print-&