Php returns null JSON when accented in MySQL

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 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?

    
asked by anonymous 30.03.2016 / 19:52

5 answers

1

The simplest solution found is to add these two lines:

$str = str_replace('\u','u',$decoded);
$strJSON = preg_replace('/u([\da-fA-F]{4})/', '&#x;', $str);

Stay tuned for your PHP version, because I use 5.5 and in tests I did not have this problem! My tests looked like this:

echo json_encode($json, JSON_UNESCAPED_UNICODE);
  

["id": "1", "name": "Porto Alegre", "state": "RS"   of "January", "state": "RJ"}, {"id": "3", "name": "Are   Paulo "," state ":" SP "}]

echo json_encode($json);
  

["id": "1", "name": "Porto Alegre", "state": "RS"   of "January", "state": "RJ"}, {"id": "3", "name": "S \ u00e3o   Paulo "," state ":" SP "}]

    
30.03.2016 / 21:18
1

Try something like this:

<?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))
       {

        while($row = $result->fetch_object())
        {
            foreach($row as $key => $col){
               $col_array[$key] = utf8_encode($col);
            }
            $row_array[] =  $col_array;

        }
        echo json_encode($row_array);
    }
    mysqli_close($con);
    ?>
    
30.03.2016 / 20:56
0

Friend, you can use the mb_convert_encoding function to convert the encoding

// In this case assuming the default is in ISO-8859-1 and you want UTF-8 mb_convert_encoding ($ utf8, 'UTF-8', 'ISO-8859-1');

    
30.03.2016 / 20:19
0

A line before echo json_encode($resultArray) , puts this:

$resultArray = array_map('utf8_encode', $resultArray);
    
30.03.2016 / 22:58
0

Before returning you call a function to convert the array to utf-8

$result = converteArrayParaUtf8($result); //chama a função
return json_encode($result); //retorna

function:

function converteArrayParaUtf8($result){
    array_walk_recursive($result, function(&$item,$key){
         if (!mb_detect_encoding($item, 'utf-8', true)) {
                $item = utf8_encode($item);
            }
    });
    return $result;
}
    
20.03.2018 / 14:26