简体   繁体   English

使用 PHP (Wordpress) 显示查询结果

[英]Display results from a query with PHP (Wordpress)

Trying to display results from an sql query in PHP:尝试在 PHP 中显示 sql 查询的结果:

  SELECT *
    FROM wp_celebcount
ORDER BY count DESC

I'm trying to display two columns: wp_celebcount.name & wp_celebcount.count我试图显示两列: 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.无法使用 PHP 显示结果 - 我想在我的 index.php Wordpress 主题文件中显示此结果。 Thanks for the help...谢谢您的帮助...

If you're using Wordpress, it would be something like this:如果您使用的是 Wordpress,它将是这样的:

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.建议使用$wpdb全局$wpdb因为它会为您处理所有数据库设置。

More information on $wpdb can be found here .可以在此处找到有关$wpdb更多信息。

Presuming you've done something like $resultSet = mysql_query('SELECT * FROM wp_celebcount ORDER BY count DESC');假设你已经做了类似 $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'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM