Sara Z Sara Z - 19 days ago 8
MySQL Question

Array stores only one value instead of multiple

I could not figure out how to solve this:

Database:

+-------+-------------+------+
| id | ArticleNr | Size |
+-------+-------------+------+
| 1 | AN111 | L |
| 2 | AN111 | XL |
| 3 | AN222 | M |
+-------+-------------+------+


sql = "SELECT * FROM table GROUP BY article_number"
$result = $site_db->query($sql);
my_array = array();

while ($row = $site_db->fetch_array($result)) {
$article_number = $row['article_number'];
$size = $row['size'];
$my_array[${'article_number'}] .= $row['size'];
}


As results I got this:


AN111: L

AN222: M


However I want something like this:


AN111: L - XL

AN222: M


Any help will be appreciated

Answer

I am guessing you want something like this:

SELECT article_number, GROUP_CONCAT(size)
FROM table
GROUP BY article_number;

Unless you really, really, really know what you are doing, don't use SELECT * with GROUP BY. It is not allowed in most other databases and is usually a sign of broken code.

Comments