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 MySQL server table whose name was passed by the parameter, but when it has an accent or an "ç" for example it returns null
MySQL is accepting the accent, I can check this by phpmyadmin, the same problem is in php.
For a cities table 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 should be São Paulo it returns null
I have already tried using json_encode with the constant JSON_UNESCAPED_UNICODE to literally encode the characters 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?