I am referring to this question here on stackoverflow, and I have the same problem, except that I use postgre database and can't seem to get this working.

This is my php function which is querying the database:

public function getCashData($id, $date)
{
    if ($this->openConnection()){
        $query = "SELECT * FROM cash_register (". $id .", '". $date ."');";

        $result = pg_query($query);
        if (!$result){
                return false;
        }

        return pg_fetch_all($result);
    }
}

I call this function like this:

$cashReport = getCashReport($id, $date);

$cashReport = array_map('utf8_encode' , $casaReport); //**note: please read below
echo json_encode($casaReport);

**This was working perfectly when I was returning only one row of result (and not all like now), but now when I'm returning an array of rows this array_map function (which I found in the above mentioned link) is not working as it expects an array and not an array of arrays.

Can you guys help me solve this problem?

share|improve this question
Why do you have to run your array through utf8_encode? I think json_encode will handle that. – Shef Jul 27 '11 at 7:36
@Shef: actually, it won't because of the reasons explain in the link I gave in the question. Just to give you an example, I will get output: "colin":null,"colout":"1500". And you see this null? Well, this is giving me problems later in the script. I need this to be "colin":"","colout":"1500". So "" instead of null. – Nikola Jul 27 '11 at 7:39
You don't solve your problems with more problems, but with solving what is causing it in the first place. Change your database collation to UTF8 if you know you will be getting utf8 characters. This will solve all the problems from the root. – Shef Jul 27 '11 at 7:43
@Shef: actually, I am not having any utf8 characters in any of my tables, and also, to be noted, I have my database set to this collation: en_US.UTF-8. I just used that solution from the (again) above mentioned link and that worked, that's why I was trying it this time also. – Nikola Jul 27 '11 at 7:56

3 Answers

Try this:

function encode_items(&$item, $key)
{
    $item = utf8_encode($item);
}

array_walk_recursive($cashReport, 'encode_items');
share|improve this answer

It's also possible to "tell" the postgre server to send the data utf-8 encoded.
see http://docs.php.net/pg_set_client_encoding

share|improve this answer
thank you for your answer but this unfortunately didn't help. – Nikola Jul 28 '11 at 8:21
up vote 0 down vote accepted

I did this in the end as I was forced to "fix" it asap:

$json = json_encode($cashReport);
$jsonEscapedNulls = str_replace('null', '""', $json);
echo $jsonEscapedNulls;

Now, I understand that "fixes" aren't solutions but I had no other choice in a give period of time :(. So if someone gives an answer which will work not as a fix but as a general solution I will be grateful. Until then, this will have to do.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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