Cannot get wpdb data (Error in a simple fuction)

I'm using Cryto Plugin and want to get coin price data from plugin db.

For easy practice, I'm trying to get BTC data.

?php function get_coin_price($coin_symbol) {
  global $wpdb;

  $coin_price = $wpdb-get_var($wpdb-prepare(SELECT price FROM {$wpdb-prefix}cmc_coins_v2 WHERE symbol = '%s', $coin_symbol));

  return $coin_price;
}?

And if I want to get BTC price, I use this code.

?php get_coin_price(BTC); ?

However I'm getting this error
Use of undefined constant BTC - assumed 'BTC' (this will throw an Error in a future version of PHP)
Is there a problem in data type? or maybe am I not using $wpdb-get_var or wpdb-prepare correctly?

Topic mysql functions wpdb plugins Wordpress

Category Web


should be a comment but can't.
your error just says that in <?php get_coin_price(BTC); ?> BTC is considered a variable but is not defined and compiler assume the variable as a string, it doesn't seems to crash your script, but still should be used like so <?php get_coin_price("BTC"); ?>


What about changing BTC to a string <?php get_coin_price('BTC'); ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.