bircastri
bircastri

Reputation: 2167

How can print a json response with php

I'm building a simil web-service in php. This web service can read all record from a table of database and return a list of it in json format.

This is the code of my getArticoli.php file:

<?php
require_once('lib/connection.php');
$query_Articolo = "SELECT CodArticolo,NomeArticolo,Quantita,CodiceBarre, PrezzoAttuale, PrezzoRivenditore,PrezzoIngrosso 
         FROM VistaArticoli ";

$result_Articoli = $connectiondb->query($query_Articolo);
$answer          = array ();
while ($row_Articoli = $result_Articoli->fetch_assoc()) {
    $answer[] = ["id"                => $row_Articoli['CodArticolo'],
                 "nome"              => '"' . $row_Articoli['NomeArticolo'] . '"',
                 "quantita"          => $row_Articoli['Quantita'],
                 "codiceBarre"       => $row_Articoli['CodiceBarre'],
                 "codartFornitore"   => $row_Articoli['CodiceBarre'],
                 "PrezzoAttuale"     => $row_Articoli['PrezzoAttuale'],
                 "prezzoRivenditore" => $row_Articoli['prezzoRivenditore'],
                 "prezzoIngrosso"    => $row_Articoli['prezzoIngrosso']];
}
//echo "fine";
echo json_encode($answer);
?>

Now, if I try to open this page, with this url: http://localhost/easyOrdine/getArticoli.php

I don't get the json_response.

In the table of database, there are 1200 records. If I try to insert an echo message in while cycle, I see it.

I have noticed that the problem lays with this field:

"nome"=>'"'.$row_Articoli['NomeArticolo'].'"'

If I remove this field from the response, I can correctly see the json response.

In this field there are any character from a-z/0-9 and special character like "/ * ? - and other".

It is possible that these special character can cause any error of the json answer?

EDIT I have limit at 5 my query and this is the response:

 [{"id":"878","0":"ACCESSORIO PULIZIA PUNTE DISSALDANTE 3 MISURE","quantita":"1","codiceBarre":"DN-705100","codartFornitore":"DN-705100","PrezzoAttuale":"14.39","prezzoRivenditore":null,"prezzoIngrosso":null},
{"id":"318","0":"ACCOPPIANTORE RJ11 TELEFONICO VALUELINE VLTP90920W","quantita":"20","codiceBarre":"5412810196043","codartFornitore":"5412810196043","PrezzoAttuale":"0.68","prezzoRivenditore":null,"prezzoIngrosso":null},
{"id":"320","0":"ACCOPPIATORE AUDIO RCA VALUELINE VLAB24950B","quantita":"5","codiceBarre":"5412810214136","codartFornitore":"5412810214136","PrezzoAttuale":"1.29","prezzoRivenditore":null,"prezzoIngrosso":null},
{"id":"310","0":"ACCOPPIATORE RJ45 VALUELINE VLCP89005W","quantita":"8","codiceBarre":"5412810228843","codartFornitore":"5412810228843","PrezzoAttuale":"0.38","prezzoRivenditore":null,"prezzoIngrosso":null},
{"id":"311","0":"ACCOPPIATORE USB2 VALUELINE VLCP60900B","quantita":"5","codiceBarre":"5412810179596","codartFornitore":"5412810179596","PrezzoAttuale":"1.80","prezzoRivenditore":null,"prezzoIngrosso":null}]

Upvotes: 0

Views: 60

Answers (1)

yivi
yivi

Reputation: 47369

First, remove those extraneous quote characters. You don't need them and they could hurt you down the road:

while ($row_Articoli = $result_Articoli->fetch_assoc()) {
    $answer[] = [
                 "id"                => $row_Articoli['CodArticolo'],
                 "nome"              => $row_Articoli['NomeArticolo'],
                 "quantita"          => $row_Articoli['Quantita'],
                 "codiceBarre"       => $row_Articoli['CodiceBarre'],
                 "codartFornitore"   => $row_Articoli['CodiceBarre'],
                 "PrezzoAttuale"     => $row_Articoli['PrezzoAttuale'],
                 "prezzoRivenditore" => $row_Articoli['prezzoRivenditore'],
                 "prezzoIngrosso"    => $row_Articoli['prezzoIngrosso']
               ];
}

Then, run your query as is (without the LIMIT), and afterwards run echo json_last_error_msg()';, which could give you a hint to what's going on.

Also, being that your DB is in italian, maybe its encoding is not UTF-8. json_encode requires UTF-8 encoded data. So you may try to utf8_encode your article names before json_encoding the array:

             "nome"              => utf8_encode($row_Articoli['NomeArticolo']),

And see what you get.

Changing your DB charset to 'utf8mb4' could do the trick as well, and it is usually recommended.

Upvotes: 2

Related Questions