Northern Flame Posted February 27, 2008 Share Posted February 27, 2008 I have values from an array which are a numbered values. for example: $array = array(1, 45, 67, 22, 86); i want to put them into a column of my database like this: 1,45,67,22,86 just like that, all the numbers in the same column but separated by commas. how will i do that? will it be something like this: $sql = "UPDATE table SET numbers = '"; foreach($array as $val){ $sql .= "$val,"; } $sql .= "' WHERE id='$id'"; mysql_query($sql); is there a smarter way of doing this? or is that the correct way? Link to comment https://forums.phpfreaks.com/topic/93261-values-from-an-array-putting-into-database/ Share on other sites More sharing options...
drisate Posted February 27, 2008 Share Posted February 27, 2008 For a one dimensional array, use the implode() function: $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone Link to comment https://forums.phpfreaks.com/topic/93261-values-from-an-array-putting-into-database/#findComment-477681 Share on other sites More sharing options...
Northern Flame Posted February 27, 2008 Author Share Posted February 27, 2008 oh haha thanks! i totally forgot about the implode() function! Link to comment https://forums.phpfreaks.com/topic/93261-values-from-an-array-putting-into-database/#findComment-477682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.