I'm trying to figure out how to save SQL query results into array:
This loop only produces 1 result in array:
<?php
$term = $_GET['term'];
$query = "SELECT * FROM Bob WHERE h_city LIKE '%".$term."%' ORDER BY
h_city ASC";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)) {
$result = $row['h_city'];
array_push($result);
}
$json = json_encode(array($result));
echo $json;
?>
result is: ["example1"] How to save query into array like ["example1","example2"]. Any thoughts what I might be doing wrong, or anyone can point me to the right direction?
EDIT as it was mentioned in the comments this way of doing queries is prone to SQL injections: more info here more info on converting results to arrays: more info here
array_push($result);makes little sense. You need to supply at least two parameters, starting with the second one, those are the values you actually want to push into the array.$resultis your query result then on first loop iteration you destroy it$result = $row['h_city'];$resultis already being used as the resource for your query, and you're overwriting$result. The array push is just wrong here.