Reputation: 1176
I'm building a REST API and I don't know how to echo several values from one column, I want to echo several values from one single column to a JSON array like the example below. The column will be used to store the usernames a users friends. How do I echo this as the example below and how do I store it in the mysql using PHP?
{
"friends":[
"tim",
"jesper",
"wexx"
]
}
Upvotes: 0
Views: 180
Reputation: 329
This is how to get the values from MySQL and convert them to JSON format
$conn = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$db = mysql_select_db(DB_DATABASE) or die(mysql_error());
// get the info from the db
$sql = "SELECT names FROM friends ORDER BY id";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
$output[]=$row;
}
print(json_encode($output));
Upvotes: 1