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

4
  • 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) Commented Dec 5, 2017 at 18:40
  • 2
    var_dump($currency_real) is your friend when trying to debug problems like this. Commented Dec 5, 2017 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 stackoverflow.com/questions/486757/… Commented Dec 5, 2017 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. Commented Dec 5, 2017 at 18:59

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Actually your right, the OP changed the code in the method, not once out.. moving on..
@LawrenceCherone thanks for the hint, updated to mimic the data format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.