$return_arr = array();
$fetch = mysql_query("select `menu_item_name` from menu_option");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['id'] = $row['menu_item_name'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
2 Answers
Change this:
From
array_push($return_arr,$row_array);
To
$return_arr = array_push($return_arr,$row_array);
1 Comment
Run your SQL first and ensure that what you're getting back is in fact a result.
This can be done by either running
select menu_item_name from menu_option
manually in mysql shell or through something like MySQL Workbench.
Also, a print_r($return_arr) won't hurt to debug the issue you're having as the code itself should work fine. JSON_ENCODE works. The problem you're having is that you don't have an array.
Side note: It's better to use some form of ORM instead of writing low-level SQL for yourself. Have a look into Doctrine, Propel or Redbean, and save yourself some headaches.