0

I am trying to use PHP to return tName from the database and then output it in the following way: {"tName":["Name1", "Name2"]}) So far it is returning ["Name1", "Name2", "Name3"] but that is not what I want.

Here is my current code

$q = mysql_query("SELECT tName FROM Template");
$result = array();
while($e=mysql_fetch_array($q, MYSQL_ASSOC)){
$result[]=$e['tName'];
} 
echo json_encode($result);
1
  • 1
    json_encode(array("tName" => $result))? Commented May 2, 2014 at 2:16

1 Answer 1

1

you just need to add the key.

$result['tName'][]=$e['tName'];

or

json_encode(array("tName" => $result))
Marty
39.5k19 gold badges97 silver badges164 bronze badges
answered May 2, 2014 at 2:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.