tags:

views:

66

answers:

3

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  A: 

mysql_fetch_assoc

hsz
+1  A: 

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'];
}
preinheimer
Interesting.. Results are printing like this:array(2) { ["Name"]=> string(11) "Tiger Woods" ["Count"]=> string(3) "124" } array(2) { ["Name"]=> string(3) "Eve" ["Count"]=> string(2) "54" } array(2) { ["Name"]=> string(7) "Rihanna" ["Count"]=> string(2) "46" }......
Mike
That's the output of var_dump(). You could try echo '<pre>';print_r( $row );echo '</pre>';That should make it more readable.
Jacob Relkin
OK..! This is better. Thanks. Here's the page.. http://www.celebrything.com/I'm trying to print the results to just show the values of names,counts without the labels.. Any ideas?
Mike
+2  A: 

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.

dragonmantank
For this to work, the table would need to be in the same database with the other WordPress tables.
Michael
Correct. But considering that the table name is 'wp_celebcount', I'm going to bet that it is :P
dragonmantank
Yes - this worked. Thanks!
Mike