How to check if metakey exist wordpress


Staff member
Basicly i want to check if a row exist in the metadata in wordpress. If the metakey table for that user does not exist it should make one.
This code i am using right now and it works but it seems buggy. I have recently notice that some users have two to five meta_key called credits now with value 0. Is there a better way to check if a meta_key already exist for the user or not?

global $current_user;
global $wpdb;


if (empty($credits)) { 

$wpdb->insert( "wp_usermeta", array( 'umeta_id' => null, 'user_id' => $useridvip, 'meta_key' => credits, 'meta_value' => 0 ) );

else { echo "all good"; }