Display results from a query with PHP (Wordpress)

17,951

Solution 1

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.

Solution 2

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

Solution 3

mysql_fetch_assoc

Share:
17,951
Mike
Author by

Mike

Updated on June 14, 2022

Comments

  • Mike
    Mike almost 2 years

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

  • Mike
    Mike over 14 years
    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" }......
  • Jacob Relkin
    Jacob Relkin over 14 years
    That's the output of var_dump(). You could try echo '<pre>'; print_r( $row ); echo '</pre>'; That should make it more readable.
  • Michael
    Michael over 14 years
    For this to work, the table would need to be in the same database with the other WordPress tables.
  • Mike
    Mike over 14 years
    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?
  • dragonmantank
    dragonmantank over 14 years
    Correct. But considering that the table name is 'wp_celebcount', I'm going to bet that it is :P