I’m trying to put an mysql query result into an string
I tried to find an answer but all the similar posts were getting subquery answers which is not what I’m trying to do.
for example
+----------------------------------+
| Fruits_tbl |
+----+---------+---------+---------+
| ID | Fruit | Color | Number |
+----+---------+---------+---------+
| 1 | Orange | Orange | 3 |
| 2 | Apple | Red | 5 |
+----+---------+---------+---------+
$sql = "select Fruits,Color,Number from Fruits_tbl where ID = 2";
$result = $pdo->query($sql);
$row = $result->fetch();
print_r($row);
This will give me something like
Array([0]=>”Apple”, [1]=>”Red”, [2]=>”5″, [Fruit]=>”Apple”, [Color]=>”Red”, [Number]=>”5″)
implode will give me 2 of each
I want just need a string = “Apple, Red, 5”
what I currently have is
$string = $row['Fruit'].", ".$row['Color'].", ".$row['Number']
As you can see that’s rather tedious.
Is there something like implode but only return the index array or something?
You can modify your fetch to get just an associative array:
Or, you can get a list:
Either of those could be imploded for single values, and would not contain the overhead that an array_unique() would.