I am starting with a new site (it's my first one) and I am getting big troubles ! I wrote this code
<?php
include("misc.inc");
$cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
$query="SELECT DISTINCT country FROM stamps";
$result=mysqli_query($cxn,$query) or die ("couldn't execute query");
$numberOfRows=mysqli_num_rows($result);
for ($i=0;$i<$numberOfRows;$i++){
$row=mysqli_fetch_assoc($result);
extract($row);
$a=json_encode($row);
$a=$a.",";
echo $a;
}
?>
and the output is as follows :
{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"},
which should be a correct JSON outout ...
How can I get it now in Jquery ? I tried with
$.getJSON
but I am not able to fuse it properly. I don't want yet to pass the data to a DIV or something similar in HTML.
As an update, the code of Andres Descalzo works !
<?php
include("misc.inc");
$cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
$query="SELECT DISTINCT country FROM stamps";
$result=mysqli_query($cxn,$query) or die ("couldn't execute query");
$numberOfRows=mysqli_num_rows($result);
echo "{data: [";
for ($i=0; $i<$numberOfRows; $i++){
$row=mysqli_fetch_assoc($result);
extract($row);
$a = (($i!=0)?",":"") . json_encode($row);
echo $a;
}
echo "]}";
?>
The output is correct and is as follows :
{data: [{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"}]}
How can I use $getJSON ?
It's ok that the syntax is
$.getJSON( url, [ data ], [ callback(data, textStatus) ] )
and that the url is the above mentioned PHP file but [data] and callback function?