Title: Counting Rows Returned
Last modified: August 18, 2016

---

# Counting Rows Returned

 *  [maxweisel](https://wordpress.org/support/users/maxweisel/)
 * (@maxweisel)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/)
 * How would I count rows returned if I want to use a SELECT query. I have no idea
   how to run queries using wordpress’s functions and what I want to do it see if
   there are zero rows returned. so what would this translate to if I wanted to 
   use wordpress functions.
 * <?php
    $myvar = mysql_query(“SELECT * FROM wp_visitorip WHERE ip=’$user_ip'”);
   $myvar = mysql_num_rows($myvar); if($myvar = 0) { // no rows } else { // at least
   1 row } ?>
 * Max

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Thread Starter [maxweisel](https://wordpress.org/support/users/maxweisel/)
 * (@maxweisel)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371055)
 * this would be in a function
 *  Thread Starter [maxweisel](https://wordpress.org/support/users/maxweisel/)
 * (@maxweisel)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371057)
 * EDIT this would be inside a plugin not a function**
 * Max
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371059)
 * You can just test on $myvar:
 * `if($myvar) {`
 * It should return `false` if it contains nothing.
 * By the way, you might want to look into using the wpdb class in your plugin instead
   of PHP’s regular MySQL functions:
 * [http://codex.wordpress.org/wpdb_Class](http://codex.wordpress.org/wpdb_Class)
 *  Thread Starter [maxweisel](https://wordpress.org/support/users/maxweisel/)
 * (@maxweisel)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371063)
 * that is what I am talking about I have no idea how to use those. is there anyway
   you can translate my snipet of code to use the wpdb class
 * Max
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371067)
 * First, scope `$wpdb` to global:
 * `global $wpdb;`
 * From there use:
 * `$myvar = $wpdb->get_results("SELECT * FROM wp_visitorip WHERE ip='$user_ip'");`
 * Also, if you’d like the plugin to use the blog’s table prefix for your ‘visitorip’
   table, scope `$table_prefix` to global as well, then assign it like so:
 * `$wpdb->visitorip = $table_prefix . 'visitorip';`
 * Your query line then becomes:
 * `$myvar = $wpdb->get_results("SELECT * FROM $wpdb->visitorip WHERE ip='$user_ip'");`
 *  Thread Starter [maxweisel](https://wordpress.org/support/users/maxweisel/)
 * (@maxweisel)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371077)
 * so then myvar would equal a number.
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371079)
 * No, $myvar would equal an array holding the results of your query: one or more
   table records where each record’s ip column matched $user_ip. But if there’s 
   no match, there’s no array.
 *  [lunabyte](https://wordpress.org/support/users/lunabyte/)
 * (@lunabyte)
 * [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371128)
 * If you need the results of the query later on, you don’t want to make the myvar
   variable hold osmething else. You may or may not need the results, just a mention.
 * If you do NOT need the results, try this:
 * <?php
    $myvar = $wpdb->get_results(“SELECT COUNT(*) FROM wp_visitorip WHERE ip
   =’$user_ip'”);
 * if($myvar == 0)
    { // no rows, do whatever. } else { // at least 1 row, do something
   else. } ?>
 * If you do need the result of the query, do this instead.
 * $myvar = $wpdb->get_results(“SELECT * FROM wp_visitorip WHERE ip=’$user_ip'”);
 * $myvarResults = mysql_num_rows($myvar);
    if($myvarResults == 0) { // no rows,
   do whatever. } else { // at least 1 row, do something else. // here you could
   run a while loop, whatever. } ?>
 * There’s tons of ways to do it. You can check the
    myvar variable in tons of way.
 * Like, if (!is_array($myvar)),
    do a num rows, do a count query, whatever.
 * Either way, if you do a check for = x, remember in a
    check if something =’s 
   a value, to use double equal signs, ($x == $y) or even triple in some cases.
 * When in doubt, php.net is a great reference for looking
    up things you might 
   not use everyday.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Counting Rows Returned’ is closed to new replies.

 * 8 replies
 * 3 participants
 * Last reply from: [lunabyte](https://wordpress.org/support/users/lunabyte/)
 * Last activity: [20 years, 2 months ago](https://wordpress.org/support/topic/counting-rows-returned/#post-371128)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
