php - Empty json_encode -
below php code. makes query on database, puts data array , , try pass json , print these same data.
the problem print coming out empty. using print_r
can print data array, when pass data json , return null. how can fix this?
<?php $user = "root"; $password = ""; $db = "angulardb"; $host = "localhost"; $con = mysqli_connect("localhost", $user, $password, $db); if (mysqli_connect_errno()){ echo "failed connect mysql: " . mysqli_connect_error(); } $usuario = mysqli_query($con, "select * users"); if (!$usuario) { echo "não foi possível executar consulta no banco de dados: " . mysql_error(); exit; } $return = array(); while ($dados = mysqli_fetch_assoc($usuario)) { $return[] = $dados; } header('content-type: application/json'); echo json_encode($return); ?>
database:
create table if not exists `users` ( `id` int(6) not null auto_increment, `nome` varchar(50) not null, `email` varchar(50) not null, `pass` varchar(50) not null, primary key (`id`) ) engine=innodb default charset=latin1 auto_increment=4 ; insert `users` (`id`, `nome`, `email`, `pass`) values (2, 'joão silva', 'joao.silva@angular.com', '123456'), (3, 'mario de almeida', 'mario.almeida@angular.com', '123456');
this due fact, data returned database might contain non-utf-8 character.
you can try :
json_encode($data, json_unescaped_unicode)
or
json_decode($json, false, 512, json_unescaped_unicode)
also try out answer in link : how json_encode() work iso-8859-1 (åäö)
Comments
Post a Comment