0

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?

2 Answers 2

1

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.
0

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));

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.