Abc
Abc

Reputation: 19

Json encode and special characters

My code was working fine when i was getting the data from a table without special characters, but when i tried to use the same code, on a different table, and Json returned me null values, how can i fix that, and output the data with special characters?

if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

mysql_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM dates");

while ($row = mysql_fetch_assoc($result))
    {
    $output[] = $row;
    }

$users = json_encode(array(
    "contacts" => $output
));
mysql_close($con);

Thanks

Upvotes: 0

Views: 58

Answers (1)

Chris Brendel
Chris Brendel

Reputation: 700

json_encode expects the data to be UTF-8 encoded (see here). Try getting MySQL to encode your query response as UTF-8.

mysql_select_db("mydb", $con);
mysql_set_charset('utf8', $con);
$result = mysql_query("SELECT * FROM dates", $con);

Please note that the mysql functions are deprecated; look into the mysqli functions.

Upvotes: 1

Related Questions