• Hello,

    each time I am sending such a request,

    $result=$wpdb->query( $wpdb->prepare( "DELETE FROM wp_usermeta 
    				WHERE wp_usermeta.user_id = " . $user_id . " 
    				AND wp_usermeta.meta_key = '" . $meta_key . "' " ));

    I have a warning like this:
    Notice: Undefined offset: 0 in /home/vkgtejtd/public_html/wp-includes/wp-db.php on line 1323
    The request is correctly executed. The result value is 0
    What’s the matter ?

    • This topic was modified 3 years, 3 months ago by alexandraka.
Viewing 1 replies (of 1 total)
  • You’re using that function the wrong way. To properly “prepare” the string, you need to pass the values to the function, not include them in the main query string. The error that you’re seeing is because you’re not passing anything in.

    This should be more like what you need (assuming that user ID os a number and meta key is a string):

     $wpdb->prepare( "DELETE FROM ".$wpdb->usermeta." 
    				WHERE ".$wpdb->usermeta.".user_id = %d 
    				AND ".$wpdb->usermeta.".meta_key = %s", $user_id, $meta_key);

    You’ll note that Ive also set the table from the database class too. That way it uses the correct prefixing if that ever changes for any reason.

    But… there’s also a better built-in way to do this:

    $wpdb->delete ($wpdb->usermeta, array (
        'user_id' => $userid,
        'meta_key'=> $meta_key
    ), array (
        '%d',
        '%s'
    ));

    Or… even better, and a lot easier:

    delete_user_meta ($user_id, $meta_key);

Viewing 1 replies (of 1 total)
  • The topic ‘Notice: Undefined offset: 0 in /…/wp-includes/wp-db.php’ is closed to new replies.