Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 325
user3030176 Avatar asked Nov 25 '13 05:11

user3030176


People also ask

How do I run a query in WordPress?

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.

Can I use MySQL with WordPress?

Using MySQL for WordPress MySQL uses table structures to store data. Most web hosts come with a MySQL user interface software called phpMyAdmin. This free and open-source piece of software makes it easy to run database commands.

How do I run a MySQL query?

You can execute a MySQL query towards a given database by opening the database with phpMyAdmin and then clicking on the SQL tab. A new page will load, where you can provide the desired query. When ready click on Go to perform the execution. The page will refresh and you will see the results from the query you provided.

How do I start MySQL database in WordPress?

Click on the button on the left panel labeled 'Go to LPCP'. Go to MySQL Manager. Add the user name and database name but leave the host name as the default IP number. Note the IP address of the database on the right which is different from the default IP number of the host indicated in the above step.


2 Answers

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

Read this.

like image 107
Narek Avatar answered Oct 13 '22 21:10

Narek


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.

like image 45
jaydeep namera Avatar answered Oct 13 '22 20:10

jaydeep namera