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

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

For this to work, the table would need to be in the same database with the other WordPress tables.
Correct. But considering that the table name is 'wp_celebcount', I'm going to bet that it is :P
1

mysql_fetch_assoc

Comments

1

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'];
}

3 Comments

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" }......
That's the output of var_dump(). You could try echo '<pre>'; print_r( $row ); echo '</pre>'; That should make it more readable.
OK..! This is better. Thanks. Here's the page.. celebrything.com I'm trying to print the results to just show the values of names,counts without the labels.. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.