Becky Becky - 1 year ago 66
SQL Question

Getting all records json_encode

I'm trying to get all data from table 1 in json format.

$arr = array();
$sql = "SELECT * FROM table1 ORDER BY price DESC";
$statement = $connect->prepare($sql);
$result = $statement->fetchAll();

foreach ($result as $val){
$arr['id'] = $val['item'];
$arr['price'] = $val['price'];
echo json_encode($arr);



The problem is that I get only one record. Why isn't the
not sending all the records?

Answer Source


foreach ($result as $val){
 $arr[] = array(
  "id" => $val['item'],
  "price" => val['price']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download