0
Hello, I have the following code in php:
<?php
$con=mysqli_connect("localhost","user","senha","banco");
$parametro = $_GET["parametro"];
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " .mysqli_connect_error(); }
$sql = "SELECT * FROM " . $parametro;
if ($result = mysqli_query($con, $sql))
{
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object())
{
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo json_encode($resultArray);
}
mysqli_close($con);
?>
It returns a JSON with a table of the Mysql server whose name was passed by the parameter, but when it has an accent or a "ç" for example it returns null
Mysql is accepting the accent, I can check it by phpmyadmin, the problem is even in php.
For a table cities it returns this:
[{"id":"1","nome":"Porto Alegre","estado":"RS"},{"id":"2","nome":"Rio de janeiro","estado":"RJ"},{"id":"3","nome":null,"estado":"SP"}]
Where São Paulo should be he returns null
I have tried using json_encode with JSON_UNESCAPED_UNICODE constant to encode characters literally instead of escaping them.
echo json_encode($resultArray, JSON_UNESCAPED_UNICODE);
but this error appears: Warning: json_encode() expects exactly 1 parameter, 2 given in /home/diretorio/public_html/busca.php on line 24
how to solve this?
But how do I turn "S u00e3o Paulo" into "São Paulo"? I put
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
but still didn’t give @Jhonnyjks– Athila Zuma
Dude, it would be best to update your php to at least 5.4.0, which will then accept the JSON_UNESCAPED_UNICODE parameter, or you should find some function that decodes from utf8, but conventional ones didn’t work here.
– Jhonny Mesquita
These two lines have now solved: $str = str_replace(' u','u',$decoded); $strJSON = preg_replace('/u([ da-fa-F]{4})/', '&#x 1;', $str); I found it at: http://stackoverflow.com/a/20181671/5664531
– Jhonny Mesquita