3

here is my php code

function GetTransactionList($data)
{
    // DB transaction   
    $records = array();
    while($row = mysql_fetch_array($result))
        array_push($records, $row);

    echo json_encode(array("IsError" => false, "Records" => $records));

}

this is my json response in ajax call from php

{
    IsError:false,
    Records:[
            {0:1,
             1:1000,
             2:0,
             3:"Peacock India trial payment",
             4:"2013-08-03",
             5:1,
             TransactionID:1,
             Credit:1000,
             Debit:0,
             Reason:"Peacock India trial payment",
             TransactionDate:"2013-08-03",
             TransactionByUserID:1
            }]
}

here I got my result but json_encode() method encodes each row twice first it sets values by index=>value pair and second time it encodes by column_name=>value pair. I want to know why it happens? Is there any way to reduce this double work.? I want json response only as following way

{
    IsError:false,
    Records:[
            {TransactionID:1,
             Credit:1000,
             Debit:0,
             Reason:"Peacock India trial payment",
             TransactionDate:"2013-08-03",
             TransactionByUserID:1
            }]
}

2 Answers 2

2

No it doesn't, it is mysql_fetch_array that gives you both associate and numeric keys.
If you only want associative keys specify it in the function mysql_fetch_array($result, MYSQL_ASSOC);

Sign up to request clarification or add additional context in comments.

1 Comment

Just an FYI, the options are MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH according to the mySQL mysql_fetch_array documentation
1

You can use mysql_fetch_assoc instead of mysql_fetch_array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.