ScrewLooseSalad Posted March 14, 2013 Share Posted March 14, 2013 I'm trying to build up a long string of variables, but my code only outputs the last entry in the MySQL database, can anyone point out my mistake? $query = "SELECT * FROM `Litem` WHERE 1"; $result = $db->query($query); $i=1; $i=1; while ($row = $result->fetch_assoc()) { if($i=1){$itemlist = "'".$row[Part_Name];} else{$itemlist .= "','".$row[Part_Name]; } $i++; } $itemlist .= "'"; echo $itemlist; Link to comment https://forums.phpfreaks.com/topic/275643-concating-an-array-of-variables/ Share on other sites More sharing options...
trq Posted March 14, 2013 Share Posted March 14, 2013 SELECT GROUP_CONCAT(Part_Name SEPARATOR "','") AS parts FROM Litem GROUP BY Part_Name Link to comment https://forums.phpfreaks.com/topic/275643-concating-an-array-of-variables/#findComment-1418558 Share on other sites More sharing options...
Barand Posted March 14, 2013 Share Posted March 14, 2013 or $query = "SELECT Part_Name FROM `Litem` "; $result = $db->query($query); $i=array(); while ($row = $result->fetch_assoc()) { $i[] = $row['Part_Name']; } $itemlist = "'" . join("','", $i) . "'"; echo $itemlist; Link to comment https://forums.phpfreaks.com/topic/275643-concating-an-array-of-variables/#findComment-1418566 Share on other sites More sharing options...
Christian F. Posted March 14, 2013 Share Posted March 14, 2013 Am I correct in my suspicion that you're trying to build up a dynamic SQL query with the item IDs? If so, then you should really be looking at JOINs. If not, then just ignore me. Link to comment https://forums.phpfreaks.com/topic/275643-concating-an-array-of-variables/#findComment-1418568 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.