Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been trying to convert the fields from a mysql_fetch_array (that are urlencoded) to urldecode before converting to JSON (json_encode)

Here's what I'm working with that doesn't work:The output is still urlencoded

$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_fetch_array(mysql_query($query));
foreach($result as $value) {
    $value = urldecode($value);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));

Any ideas?

share|improve this question

2 Answers

up vote 1 down vote accepted

yeah....! you're not updating $result with the value returned by the function. $value needs to be passed by reference.

foreach($result as &$value) {
    $value = urldecode($value);
}

or

foreach($result as $i => $value) {
    $result[$i] = urldecode($value);
}

when you do this...

foreach($result as $value) {
    $value = urldecode($value);
}

The result of the function is lost at at iteration of the foreach. You're trying to update each value stored in $result but that's not happening.

  • Also take note that the code only fetches one row from your query. I'm not sure if that's by design or not.
share|improve this answer

Try:

$query = "SELECT * FROM table WHERE tableId=$tableId";
$result = mysql_query($query);
$value = array();
while($row = mysql_fetch_array($result))
$value[] = urldecode($row);
}
$jsonOut = array();
$jsonOut[] = $result;
echo (json_encode($jsonOut));
share|improve this answer

Your Answer

 
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.