user6664057 user6664057 - 1 year ago 106
MySQL Question

How do I separate a list with a comma in a loop in PHP

I have this select statement that select contacts from a database. The selected mobile numbers are passed as recipients of a text message in SMS form. They are passed through a web service inform of strings. The mobile numbers should be separated by a comma to receive the message.
How do I achieve this such that in a scenario that there are say twenty numbers they are separated by a comma as they pass to as service arguments of the web service? Kindly, anyone?


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

$message = mysqli_real_escape_string($conn, $_POST['message']);

$time1 = $_SESSION['post-data']['t1'];
$time2 = $_SESSION['post-data']['t2'];

$sql = "SELECT DISTINCT msisdn FROM customer WHERE DATE_FORMAT(time_paid, '%Y-%c-%e') BETWEEN ADDDATE('$time1',INTERVAL 0 HOUR) AND ADDDATE('$time2',INTERVAL '23:59' HOUR_MINUTE)";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row
while($row = $result->fetch_assoc()) {
$mobilenumber = $row['msisdn'];

} else {
echo "No contacts Selected";

sendbulk($mobilenumber, $message);

function sendbulk($mobilenumber,$message) {

$serviceArguments = array(
"mobilenumber" => $mobilenumber,
"message" => $message

$client = new SoapClient("http://localhost:8080/WebSms/smswebservice?wsdl");

$result = $client->process($serviceArguments);

return $result;


Answer Source

Not sure what you meant in the question. But, based on your comment I'd say you want to convert numeric array into a comma separated string.

This is what implode() was made for:


$array = array(3446, 346587, 589);
$comma_separated = implode(",", $array);


string(15) "3446,346587,589"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download