How do I run a mysql query in wordpress?

This query works and returns the results that I want in MySQL but how do I get this to work in wordpress?

SELECT * FROM `wp_usermeta` WHERE `meta_key` = 'points' AND `user_id` = '1'

I want to be able to see all the ‘points’ earned by user 1. I need to be able to display this on posts and pages in wordpress.

Related posts

Leave a Reply

3 comments

  1. Use following code…..

    $q="SELECT * FROM wp_usermeta WHERE meta_key = 'points' AND user_id = '1'";
    $result = $wpdb->get_results($q);
    

    It will return array as result.

  2. You should try following code :

    global $wpdb;    
    $result = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."_usermeta WHERE meta_key = 'points' AND user_id = '1'");
    print_r($result);
    

    Hope it will helpful.