lee
lee

Reputation: 17

urldecode in json doesn't work

require_once('db.php');

$sql = "SELECT * FROM tbl";

$rs = mysql_query($sql); 

$rows = array();

while($r = mysql_fetch_assoc($rs)) {
    $rows['Record'][] = $r;
}

print json_encode($rows);

mysql_close($conn);

// it works perfect above

while($r = mysql_fetch_assoc($rs)) {

    $r = utf8_decode(urldecode($r));

    $rows['Record'][] = $r;
}

// it doesn't work above, may be $r is an object how can I make it works?

Upvotes: 0

Views: 346

Answers (1)

Astig NaGahum
Astig NaGahum

Reputation: 31

The problem is that mysql_fetch_assoc returns an array, and you want to pass it to urldecode, which waits for a string as a parameter.

To achieve what you want, you should call urldecode to all the columns in the returned $r.

You can do it like this, for instance:

$r = array_map(function($col){
    return utf8_decode(urldecode($col));
}, $r);

http://php.net/manual/en/function.mysql-fetch-assoc.php

http://php.net/manual/en/function.urldecode.php

Upvotes: 1

Related Questions