php - Query to Json: Return Each Column Twice with Different Names -


i have php code payments (payment amount, name of user, month of payment) deposited users in months:

$result = $mysqli->query("select p.id id, p.amount amount,                            u.name user_id, m.name month_id                            payment p, user u, month m                            p.user_id = u.id , p.month_id = m.id;");     //add records array $rows = array(); while($row = $result->fetch_array()) {     $rows[] = $row; }  //return result $jtableresult = array(); $jtableresult['result'] = "ok"; $jtableresult['records'] = $rows; print json_encode($jtableresult); 

and json get:

[{   "result": "ok",   "records": [     {       "0": "1",       "id": "1",       "1": "250",       "amount": "250",       "2": "user 1",       "user_id": "user 1",       "3": "jan 15",       "month_id": "jan 15"     }, ...] 

now, think these "0", "1", "2", "3" names/values not supposed there , must have done wrong here. doing of json_encode()? or way i'm querying db?

thanks help!

the issu here calling fetch_array().

fetch_array() return array of index based values key based values. if want key(name) based values in array, use below code.

try fetch_assoc().

$rows = array(); while($row = $result->fetch_assoc()) {     $rows[] = $row; } 

or fetch_array(mysqli_assoc)

$rows = array(); while($row = $result->fetch_array(mysqli_assoc)) {    $rows[] = $row; } 

Comments