php - Retrieve sql data and generate as json file -


i trying retrieve data mysql , generate json file.
output showing 1 row.
there many rows inside database table.
wrong code?

$sql = "select * form_element"; $result = mysqli_query($conn, $sql); $response = array(); $data_array = array(); if (mysqli_num_rows($result) > 0) {    // output data of each row    while($data = mysqli_fetch_assoc($result)) {       $id = $data['id'];       $name = $data['name'];       $email = $data['email'];       $phone = $data['phone'];       $address = $data['address'];       $data_array = array(          'name' => $name,           'email' => $email,           'phone' => $phone,           'address' =>  $address       );    } } else {    echo "0 results"; } $response['data_array'] = $data_array; $fp = fopen('results.json', 'w'); fwrite($fp, json_encode($response)); 

you're overwriting $data_array every time in loop.

so, part:

$data_array = array(   'name' => $name,    'email' => $email,    'phone' => $phone,    'address' =>  $address ); 

should changed to:

$data_array[] = array(   'name' => $name,    'email' => $email,    'phone' => $phone,    'address' =>  $address ); 

then each row added $data_array.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -