WordPress.org

Ready to get started?Download WordPress

Forums

Counting Rows Returned (9 posts)

  1. maxweisel
    Member
    Posted 8 years ago #

    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

  2. maxweisel
    Member
    Posted 8 years ago #

    this would be in a function

  3. maxweisel
    Member
    Posted 8 years ago #

    EDIT this would be inside a plugin not a function**

    Max

  4. Kafkaesqui

    Posted 8 years ago #

    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

  5. maxweisel
    Member
    Posted 8 years ago #

    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

  6. Kafkaesqui

    Posted 8 years ago #

    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'");

  7. maxweisel
    Member
    Posted 8 years ago #

    so then myvar would equal a number.

  8. Kafkaesqui

    Posted 8 years ago #

    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.

  9. lunabyte
    Member
    Posted 8 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.