• Resolved janpotuznik

    (@janpotuznik)


    Hello, I’m trying to create WooCommerce plugin which will pass all checks done by https://github.com/woocommerce/woocommerce-sniffs

    I ran these tests, results in comments:

    
    global $wpdb;
    
    $table_name = $wpdb->prefix . 'mytable';
    
    // Places table name in single quotes (') which is wrong.
    $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %s', $table_name ) );
    
    // Produces error: Use placeholders and $wpdb->prepare(); found $table_name . 
    $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS `' . $table_name . '` ) );
    
    // Produces error: Use placeholders and $wpdb->prepare(); found interpolated variable $table_name at "DROP TABLE IF EXISTS `{$table_name}`" .
    $wpdb->query( $wpdb->prepare( "DROP TABLE IF EXISTS `{$table_name}`" ) );
    

    How to do it properly?

    Solution:

    
    global $wpdb;
    $wpdb->mytable = $wpdb->prefix . 'mytable';
    
    $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->mytable . '`' );
    
    • This topic was modified 3 years, 3 months ago by janpotuznik.
    • This topic was modified 3 years, 3 months ago by janpotuznik. Reason: Solution
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Solved: Table name in backticks, valid with woocommerce-sniffs’ is closed to new replies.