Display results from a query with PHP (Wordpress)


Question

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...

1
2
10/11/2016 5:53:17 AM

Accepted 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.

9
12/6/2009 3:39:55 AM

Presuming you've done something like $resultSet = mysql_query('SELECT * FROM wp_celebcount ORDER BY count DESC');

You should be able to pull out the results with

while ($row = mysql_fetch_assoc($resultSet))
{
   var_dump($row);
   //print an element named 'name' with echo $row['name'];
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon