izbryte Posted March 16, 2011 Share Posted March 16, 2011 I have a field in my MySQL DB that holds numbers separated by a comma (example: 1, 3, 45, 137, 87). I simply want to get a count of how many numbers are in there. (Using my example the result would be 5). I'm sure this is a simple question but I don't know how. I'm able to extract the numbers but I can't get the count to read anything other than 1. Can anyone help? Link to comment https://forums.phpfreaks.com/topic/230849-need-a-count-of-comma-seperated-numbers-simple-i-think/ Share on other sites More sharing options...
phpTrainee Posted March 16, 2011 Share Posted March 16, 2011 $pieces = explode(",", $message); $result = count($pieces); Link to comment https://forums.phpfreaks.com/topic/230849-need-a-count-of-comma-seperated-numbers-simple-i-think/#findComment-1188372 Share on other sites More sharing options...
Maq Posted March 16, 2011 Share Posted March 16, 2011 The easiest way is to explode the result in PHP and count the number of elements in the array, not so sure that's the most efficient. I'm also not sure there is an elegant way to do it in SQL. Did a quick search and found a couple hacky solutions. Link to comment https://forums.phpfreaks.com/topic/230849-need-a-count-of-comma-seperated-numbers-simple-i-think/#findComment-1188373 Share on other sites More sharing options...
izbryte Posted March 16, 2011 Author Share Posted March 16, 2011 Cool, thanks! Link to comment https://forums.phpfreaks.com/topic/230849-need-a-count-of-comma-seperated-numbers-simple-i-think/#findComment-1188378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.