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.
<?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.
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():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 amysql_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:The second parameter to the
get_row
function is one of these three:stdClass
– a simple objectI 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: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: