My PHP code:
require ("connect.php");
$input = $_GET["query"];
$data = array();
$query = mysql_query("SELECT `abbr` FROM `states_list` WHERE `abbr` LIKE '{$input}%'") or die(mysql_error());
while ($obj = mysql_fetch_object($query)) {
//$json = array();
//$json['query'] = $input;
//$json['suggestion'] = $row['abbr'];
$json[] = $obj;
$data = array("query"=>"$input", "suggestion"=>array($obj));
}
header("Content-type: application/json");
//echo json_encode($data);
echo '{"query":'.$input.' "suggestion":'.json_encode($json).'}';
This is my actual response:
{"query":c "suggestion":[{"abbr":"CA"},{"abbr":"CO"},{"abbr":"CT"}]}
This is how I would like it to look like:
{"query":c "suggestion":["CA","CO","CT"]}
I can’t seem to figure out the right way to arrange the output to get what I want ;(
I’d rewrite it to this- the
whileloop creates an array consisting of suggestions, then it is encoded.P.S. Also, don’t forget to properly escape the string taken from user input in order to avoid sql injections.