最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - How does $wpdb handle COUNT(*)

programmeradmin2浏览0评论

I understand how to do simple queries and display results using $wpdb. This is my process:

<?php $sql = 'select * from wp_votes;'; ?>
<?php $votes = $wpdb->get_results($sql); ?>
<?php if ( !empty ( $votes ) ) { ?>
     <?php foreach ( $votes as $vote ) { ?> 
          <td><?php echo $vote->id; ?></td>
          <td><?php echo $vote->post_id; ?></td>
          <td><?php echo $vote->date_voted; ?></td>
     <?php } ?> 
<?php } ?> 

Now, what if my query is more complicated, where there is a COUNT(*) involved, like so:

<?php $sql = 'select wp_votes.post_id, wp_posts.post_title, count(*) from wp_votes INNER JOIN wp_posts ON wp_votes.post_id = wp_posts.id group by wp_votes.post_id order by count(*) desc;'; ?> 

This should return:

--------+------------+----------+
Post ID | Post Title | Count(*) |
--------+------------+----------+
1       |  "My post" |   6
2       |  "Hello..."|   5

Would it be OK if I do something like this?

<?php $wpdb->get_results($sql, ARRAY_N); ?> 

and then, to get the count,

<?php echo $row[2]; ?> 

EDIT: Turns out, it's actually just this simple, I don't have to do anything else $row[x] will work.

I understand how to do simple queries and display results using $wpdb. This is my process:

<?php $sql = 'select * from wp_votes;'; ?>
<?php $votes = $wpdb->get_results($sql); ?>
<?php if ( !empty ( $votes ) ) { ?>
     <?php foreach ( $votes as $vote ) { ?> 
          <td><?php echo $vote->id; ?></td>
          <td><?php echo $vote->post_id; ?></td>
          <td><?php echo $vote->date_voted; ?></td>
     <?php } ?> 
<?php } ?> 

Now, what if my query is more complicated, where there is a COUNT(*) involved, like so:

<?php $sql = 'select wp_votes.post_id, wp_posts.post_title, count(*) from wp_votes INNER JOIN wp_posts ON wp_votes.post_id = wp_posts.id group by wp_votes.post_id order by count(*) desc;'; ?> 

This should return:

--------+------------+----------+
Post ID | Post Title | Count(*) |
--------+------------+----------+
1       |  "My post" |   6
2       |  "Hello..."|   5

Would it be OK if I do something like this?

<?php $wpdb->get_results($sql, ARRAY_N); ?> 

and then, to get the count,

<?php echo $row[2]; ?> 

EDIT: Turns out, it's actually just this simple, I don't have to do anything else $row[x] will work.

Share Improve this question edited Nov 5, 2011 at 0:22 21zna9 asked Nov 4, 2011 at 16:18 21zna921zna9 3812 gold badges7 silver badges19 bronze badges 3
  • 1 I'm not sure what you are trying to ask. By OK do you mean Can my query be improved? or Will this query work? – v0idless Commented Nov 4, 2011 at 16:32
  • Will this query work? – 21zna9 Commented Nov 4, 2011 at 16:50
  • Nevermind, so it does work. – 21zna9 Commented Nov 5, 2011 at 0:21
Add a comment  | 

1 Answer 1

Reset to default 8

You can just use echo $wpdb->get_var( $sql ):

https://developer.wordpress/reference/classes/wpdb/

发布评论

评论列表(0)

  1. 暂无评论