I have a database connection that I have made with a odbc connector. The problem is that I have a field that has special characters, such as accents, and it does not recognize them.
This is my webservice:
<?php
define('CHARSET','UTF-8');
header('Content-Type: text/html; charset=UTF-8');
$usuario='';
$pass='';
$dsn='Tr3';
$conexion = odbc_connect($dsn,$usuario,$pass);
$sql="select total, name, domin from F_VENTA where Ve_FirmaCamion=true";
$rs = odbc_exec($conexion, $sql);
if (!$rs) {
exit("Error al conectar la base de datos");
}
$datos = array();
$i = 1;
while($row = odbc_fetch_array($rs)) {
$datos[] = $row;
$i++;
}
odbc_close($conexion);
$json = json_encode($datos);
echo $json;
?>
This works perfectly. But if the field "name" has some special character (accents, ñ, Ç ...) then the webservice does not return anything to me. The json leaves me blank.
I tried with:
utf8_decode($json);
and
$cadena= htmlentities($json, ENT_QUOTES, "UTF-8");
but don't work.
Any advice? Thanks!
I bring my solution to the problem in case it happens to someone else.
I have managed to solve if before coding the data to json, I pass the mb_convert_encoding()
method to the data that comes to us from the database while it traverses it. So I'll encode them in the while loop:
while($row = odbc_fetch_array($rs)) {
$datos[] = mb_convert_encoding($row, "UTF-8", "iso-8859-1");
$i++;
}