0

I'm trying to send mulitple sms and the correct format for the api to send the text messages should be (00,001,002) but with my current sql it produces this results (00,001,002,)

How can i correct my script to get this results (00,001,002)

$query_rs_p = "SELECT * FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
$totalRows_rs_p = mysql_num_rows($rs_p);
$ph=',';
do {
echo $row_rs_p['phone_number'].$ph;
} while ($row_rs_p = mysql_fetch_assoc($rs_p));
neiza
  • 265
  • 4
  • 15

3 Answers3

2

Instead of echoing it out in your loop, put it all in a string. Then trim off the last comma before you output it:

$query_rs_p = "SELECT * FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
$totalRows_rs_p = mysql_num_rows($rs_p);
$ph=',';
$string = '';
do {
$string .= $row_rs_p['phone_number'].$ph;
} while ($row_rs_p = mysql_fetch_assoc($rs_p));
$string = rtrim($string, ',');
echo $string;

Or, better yet, just build this string in your query. Much simpler and cleaner:

$query_rs_p = "SELECT group_concat(phone_number) as phine_numbers FROM contacts ORDER BY id DESC";
$rs_p = mysql_query($query_rs_p, $insurance) or die(mysql_error());
$row_rs_p = mysql_fetch_assoc($rs_p);
echo $row['phone_numbers'];
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Use rtrim function

$rowsphone =  $row_rs_p['phone_number'].$ph;
rtrim($rowsphone,',');
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
0

Probably the most direct would be to just do something like this:

$output = '';
do {
    if ($output != '')
        $output .= ',';
    $output .= $row_rs_p['phone_number'];
while ($row_rs_p = mysql_fetch_assoc($rs_p);

echo $output;
VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97