1
I have a table "payment forms" and I made an API to get all these forms of payment, but even though the database is correct, it informs that the field "name" is null in return, see:
[{"nome":"Dinheiro"},{"nome":"Cheque"},{"nome":null},{"nome":null}]
And it’s not "null" and these are the only two.
Follow my PHP:
PHP:
<?php
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');
include 'database.php';
//$cod_fornecedor=$_GET['cod_fornecedor'];
$query="SELECT
nome
FROM
formas_pagamento
ORDER BY
cod_forma_pagamento ASC";
$result=$con->query($query);
$row_cnt = mysqli_num_rows($result);
if ($result->num_rows > 0)
{
$count=0;
echo "[";
while($row = $result->fetch_assoc())
{
$count++;
echo json_encode($row);
if($count!=$row_cnt)
{
echo ",";
}
}
echo "]";
}
else
{
echo "error";
}
?>
What’s wrong with it?
What’s the difference between:
$row_cnt
and$result->num_rows
? Can display both in an ECHO?– Thiago Santos
Read this post. It may be the same problem http://stackoverflow.com/questions/8143163/php-json-encode-shows-null-instead-of-text
– Marcos Xavier
Change your query to SELECT CONVERT(name USING utf8) the name FROM formas_payment ORDER BY cod_forma_asc payment
– Rodrigo Jarouche