Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display results from a query with PHP (Wordpress)

Tags:

sql

php

wordpress

Trying to display results from an sql query in PHP:

  SELECT *
    FROM wp_celebcount
ORDER BY count DESC

I'm trying to display two columns: wp_celebcount.name & wp_celebcount.count

Having trouble getting the result to show with PHP - I want to show this result on my index.php Wordpress theme file. Thanks for the help...

like image 539
Mike Avatar asked Jan 27 '26 21:01

Mike


1 Answers

If you're using Wordpress, it would be something like this:

global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount');
foreach($result as $row) {
    echo 'Name: '.$row->name.', Count: '.$row->count.'<br/>';
}

It's recommended to use the $wpdb global as it takes care of all the database setup for you.

More information on $wpdb can be found here.

like image 84
dragonmantank Avatar answered Jan 29 '26 09:01

dragonmantank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!