I have following code:
$query = "SELECT ads.*,
trafficsource.name AS trafficsource,
trafficsource.id AS trafficsourse_id,
FROM ads
JOIN trafficsource ON ads.trafficsourceId = trafficsource.id
WHERE advertiserId = '$advertiser_id'";
$mysqli = new mysqli();
$mysqli->connect('localhost', 'root', '', 'adsbase');
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
echo "<h2>Traffic Sources: {$row['trafficsource']}</h2>";
}
This code show results like:
Traffic Sources: Example1
Traffic Sources: Example2
Traffic Sources: Example2
Traffic Sources: Example1
Traffic Sources: Example2
Traffic Sources: Example1
What I want and can’t figure out is to show results like:
Traffic Sources: Example1, Example2
So without duplicates and also all in one line.
You nearly had it:
EDIT: Query using DISTINCT
I’m assuming you only need the traffic source name, but feel free to add more columns back in if they’re required. Bear in mind though that the DISTINCT applies to a distinct combination of all rows returned, so it may be possible that you could end up with duplicate traffic sources if other selected columns differ.