Not getting correct output as expected using JSON_encode

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

stephenrodrics
New php-forum User
New php-forum User
Posts: 60
Joined: Thu Mar 14, 2013 6:10 am

Not getting correct output as expected using JSON_encode

Postby stephenrodrics » Mon Apr 08, 2013 3:32 am

Not getting correct output as expected using JSON_encode

I want [THOMAS,THOMAS ]

but output comes [{"username":"THOMAS"},{"username":"THOMAS "}]

Below is the code, can anyone help me why I get Field name also along with the output

$json = array();

$testingarray = mysql_query("SELECT username FROM $tbl_nameweek where manager = 'James' ") ;

while($row = mysql_fetch_assoc($testingarray))
{

$json[] = $row;
}
$sendArray = json_encode($json);
echo json_encode($json);

OUTPUT IS : [{"username":"THOMAS"},{"username":"THOMAS "}]
$delimiter1 = "username";

$splitcontents = explode($delimiter1,json_encode($json));

$counter = "";
echo "\n";
foreach ( $splitcontents as $element )
{

echo "\n";
print $element;
}


I want output only Thomas, I don’t know why Database fieldname is also getting added to JSON array, can anyone tell how can I only retrieve Thomas from this JSON array

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Not getting correct output as expected using JSON_encode

Postby johnj » Mon Apr 08, 2013 6:48 am

Why do you want to Json encode the SQL output? Why don't you use the output straight away?
The reason why username is added to the Json encode is because that is the field name that you have selected in the SQL.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest