Mike Mike - 2 months ago 11
PHP Question

Display results from a query with 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...

Answer

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.