0

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}]
yivi
  • 42,438
  • 18
  • 116
  • 138
bircastri
  • 2,169
  • 13
  • 50
  • 119
  • 2
    Why can't you use just `"nome"=> $row_Articoli['NomeArticolo']` and leave json parser escaping? – marian0 Jan 17 '17 at 13:55
  • You are on the right way, but I think the main problem is this '"'. and this .'"' in '"'.$row_Articoli['NomeArticolo'].'"' – Oliver Jan 17 '17 at 13:56
  • Have you tried enabling error reporting ? That might help you figure out why the JSON can't be constructed. – roberto06 Jan 17 '17 at 13:59
  • @marian0, at first time, I have write "nome"=>$row_Articoli['NomeArticolo'] after I'm try to change this code to fixed my problem. But not working – bircastri Jan 17 '17 at 14:10
  • You need to remove those quotes around "Article Name". – yivi Jan 17 '17 at 14:12
  • @Oliver, I'm try to use your code, but not works – bircastri Jan 17 '17 at 14:12
  • @roberto06, how can I enabling error reporting? – bircastri Jan 17 '17 at 14:13
  • limit your select to a few lines (say, 5) do a `var_dump` for `$answer`. (And post it in your question). – yivi Jan 17 '17 at 14:14
  • @bircastri add `ini_set('display_errors', 'on');` and `error_reporting(E_ALL | E_STRICT);` at the beginning of your file. – roberto06 Jan 17 '17 at 14:14
  • if it's a `json_encode` problem, error reporting will say nothing. `json_encode` returns `false` but do not trigger an error. – yivi Jan 17 '17 at 14:15
  • I'm try to insert your code but I don't have any message. @yivi I think that your comment it is ok, the problem is the json_encode procedure, but I'm not able to fixed it – bircastri Jan 17 '17 at 14:17
  • What code did you try to insert? update your question with your attempt, please, is not clear otherwise. – yivi Jan 17 '17 at 14:18
  • Something is wrong there. The second field (article name) has a "0" key. You are making more changes in code that the ones you reflect in your answer. And you didn't make the `var_dump`. – yivi Jan 17 '17 at 14:22

1 Answers1

2

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.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • thanks you. Your code works. Now for my database, this charset is reccomended to the italian ??? thanks – bircastri Jan 17 '17 at 14:36
  • Set it to `utf8mb4`. Check this out: http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – yivi Jan 17 '17 at 14:38