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; Quote Link to comment 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 Quote Link to comment 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; Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.