MySQL Trick: Group_Concat()

Apakah anda sering menggunakan potongan kode seperti ini:

<?php
$sql = "SELECT tag FROM contoh_table";
$rs = mysql_query( $sql );
$array_tags = array();
while( false !== ( $r = mysql_fetch_assoc($rs)))
{
    $array_tags[] = $r['id'];
}
echo "Tags: " . implode(', ', $array_tags);
?>

Saya ada satu cara yang lebih mudah.

Yaitu, dengan menggunakan fungsi bawaan dari mysql: group_concat().

<?php
$sql = "SELECT GROUP_CONCAT(tag, '') as tags FROM contoh_table";
$rs = mysql_query( $sql );
$r = mysql_fetch_assoc($rs)
echo "Tags: " . $r['tags'];
?>

Bagaimana? lebih sederhana kan?

Author: Arief Bayu Purwanto

Hello, my name is Arief Bayu Purwanto, a 24 years old father of a beautiful daughter. Interested in online programming, linux, games, and reading. Currently working on kapanlagi.com as junior developer. I live in a relatively quite and cool place called Malang. I'm available for some freelance stuff as well as some consulting job. You can see my portofolio for some previous task I've finished and some other information related to my capability. Btw, I'm plurking here.