0

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

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?

4
  • All variables in PHP begin with a $, and string values need to be encapsulated in quotation marks. Since the BTC in get_coin_price(BTC) has neither of these attributes, PHP believes it to be a constant. As this question is regarding PHP syntax rather than WordPress or it's APIs, it may be considered off-topic for our stack
    – bosco
    Commented May 18, 2022 at 23:20
  • Hey @bosco, thanks for the answer and you are right in a way. However this question contains some Wordpress terms like $wpdb or get_var function so I can not ask this questions on php forum or API forum. I hope you understand. Commented May 19, 2022 at 12:33
  • I certainly get where you're coming from! But Stack Exchange sites' scopes are much more defined than traditional web communities - per this stack's definition, questions here must detail a problem specifically regarding the WordPress software and/or it's APIs. Questions regarding general PHP syntax and errors are still specific to PHP, even when that code is used within a WordPress site - Stack Overflow is a more appropriate site for such things. In this case you weren't sure of the root cause - no harm no foul :)
    – bosco
    Commented May 20, 2022 at 5:52
  • The question will likely remain closed for the reason that the problem and solution are not pertinent to WordPress itself (and thus it's broader development community) - but I am glad that you found a solution!
    – bosco
    Commented May 20, 2022 at 5:55

2 Answers 2

1

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

1
  • Wow, it worked! Thanks a lot man! Commented May 19, 2022 at 12:34
1

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

1
  • Thanks! This worked too. Thank you @safir Commented May 19, 2022 at 12:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.