0

Evening community,

I stuck at one problem, which I can't find solution for. I would appreciate if you could give me a piece of your advice. In brief, I wrote the following function in a php-file:

public function getCurrencyReal(){

    $sql = "SELECT currency_real FROM currency WHERE currency_id = '4' limit 1";

    $query = $this->db->query($sql);

    $currency_real = $query;

    return $currency_real->row;
}

After that I added the following code in another php file, that should generate XML-list:

$currency_real = $model_module_xmlcreator -> getCurrencyReal();

and

$out .= "<test>" . $currency_real . "</test>";

As a result I've received the following thing:

enter image description here

, saying "Array".

I've realized that I'm asking for an array even though I want to get info only from one field and I actually need a string. So I changed the code a bit to

$currency_real = json_encode($query);

return $currency_real; 

and my next output was enter image description here

I believe that I miss something simple, but I can't find what (the output should be just "33.00"). Pardon me if the question is silly, I've started studying PHP not much time ago.

All best

Barmar
  • 741,623
  • 53
  • 500
  • 612
Vlad B.
  • 79
  • 2
  • 9
  • What type of class is `$this->db`? Plain PDO or MySQLi do not have a `->row` property like your thing seems to have. But based on your image (please post text not images) it is likely that you can access `return $currency_real->row['currency_real']` (since that thing is known to be an array) – Michael Berkowski Dec 05 '17 at 18:40
  • 2
    `var_dump($currency_real)` is your friend when trying to debug problems like this. – Barmar Dec 05 '17 at 18:41
  • Your problem has two parts, getting the data from the database and generating the xml. Ensure your data is in the correct format before starting to generate the xml. Use the var_dump as suggested by @Barmar then use the answer to this question for generating xml https://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php – Nkole Evans Dec 05 '17 at 18:42
  • @MichaelBerkowski , thank you, this was the solution (return $currency_real->row['currency_real']). The code was inherited, so it's pretty difficult to find what is where. – Vlad B. Dec 05 '17 at 18:59

1 Answers1

4

just change this line

$out .= "<test>" . $currency_real->rows[0]->currency_real . "</test>";

or

$out .= "<test>" . $currency_real->row->currency_real . "</test>";

and don't use json_encode

am05mhz
  • 2,727
  • 2
  • 23
  • 37