I have recently started to learn MySQL and PHP, need some help. I need to take a row from a database and display only a content of cells. Current code looks something like this:
$res = R::getAll("SELECT * FROM products WHERE ID=5");
echo json_encode($res);
As I see it, I took the query and wrote its result into the variable $res, which I then printed using the function json_encode()
The result came out to be something like this:
[{"ID":"5","name":"Falcon","price":"166.00"}]
But my intention was to get something like that:
5, Falcon, 166.00
Please help to get rid of unnecessary information in the form of column names and extra symbols.
CodePudding user response:
$res = R::getAll("SELECT * FROM products WHERE ID=5");
echo implode(', ',$res[0]); // 5, Falcon, 166.00
CodePudding user response:
$res = R::getAll("SELECT * FROM products WHERE ID=5");
$output = "";
foreach ($res as $key => $value) {
$output = $output.$value.", ";
}
$output = substr($output, 0, -2);
echo $output;
Ouput: 5, Falcon, 166.00