Select not returning rows even though data

I am trying to get access to a table created by pods which is a custom table called palyer rankings. Which is withing a page template.

Im not sure I am doing it correctly cause no matter what I do its printing the icon which it shouldnt cause their is an entry in for the player id im looking for.

Read More
<?php  
$playerId='16-18-4500000';
  $myrows = $wpdb->get_results("SELECT * FROM 4hSIc_pods_player_ranking WHERE player_id='".$playerId."'" );
$num_rows = mysql_num_rows($myrows);
    if($num_rows) {
        echo "$num_rows Rowsn";
    }

    else

        {

echo "<li> <a href='#thanks' role='button' class='btn' data-toggle='modal'><i class='fa fa-hand-peace-o'></i>'".$playerId."</a></li>";
 }

?>

I only want to print the icon if they have no pending points in the system.
enter image description here

Related posts

1 comment

  1. A few issues with your approach:

    You don’t escape your data – the wpdb class has ways of doing that for you, make sure you use them.

    Here’s how your query would be rewritten to escape the player_id with $wpdb->prepare():

    $myrows = $wpdb->get_results( $wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}pods_player_ranking
        WHERE player_id=%s", $playerId ) );
    

    The other thing is that you’re trying to use PHP’s mysql* functions on a variable that is actually an array($wpdb->get_results() returns an array and not a mysql_result).

    Also when you only expect one result from your query, you can use $wpdb->get_row() – since that will directly return the row in a format you specify. Here’s how that would look like:

    $row = $wpdb->get_row( $wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}pods_player_ranking
        WHERE player_id=%s", $playerId ), OBJECT );
    

    The second parameter to the get_row function is one of these three:

    • OBJECT – this returns the result as an instance of the stdClass – a simple object
    • ARRAY_A – this returns the result as an associative array(with the column names being the keys)
    • ARRAY_N – this returns the result as a numerically indexed array.

    I also replaced the text before pods_player_ranking with {$wpdb->prefix}, since I assumed that this was your table prefix. It’s a good habit to use the prefix property of wpdb, instead of hard-coding the prefix(if you write code that will be used by more than one site, you can run into the issue of your code not working when the prefix is different).


    The last thing that I would recommend is to use debugging(add this to your wp-config.php: define( 'WP_DEBUG', true );) when you are developing a new site, or working on new features. Sometimes this might make it very difficult/impossible to navigate the site(if the plugins or theme code produces a lot of notice/error messages), so you can use this code instead:

    if ( isset( $_GET['debug'] ) ) {
        define( 'WP_DEBUG', true );
    }
    

    This way whenever you add ?debug to the current URL, you will enable the debugging messages to be shown.

    The point is that if you had debugging enabled, you would have most-likely seen an error like this one:

    Warning: mysql_num_rows() expects parameter 1 to be resource, array given in /path/to/file.php
    

Comments are closed.