Jump to content

loop problem


rn14

Recommended Posts

The code below is supposed to loop through the mobile field in a database and the send the value of this to the variable $to. This value is then used to send a text message through clickatell. However it only sends it to one of the numbers in the database not all of them. Im fairly certain my code is correct not sure weather this has something to do with clickatell??

 

$query="SELECT `mobile` FROM `customer`";

$result= mysql_query($query); 
$num=mysql_num_rows($result);

if ($result) 
	{

          while ($array= mysql_fetch_assoc($result)) 
	  {



// Your mobile number

$to = $array[mobile];
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/39472-loop-problem/
Share on other sites

You're overlaying the previously stored value in the $to variable each time through the loop. You need to use an array here:

<?php
$query="SELECT mobile FROM customer";
$result= mysql_query($query) or die("Problem with the query<pre>$query</pre><br>" . mysql_error()); 
$num=mysql_num_rows($result);
$to = array();
if ($num > 0)
    while ($array= mysql_fetch_assoc($result)) 
        $to[] = $array['mobile'];
if (!empty($to)) echo '<pre>' . print_r($to,true) . '</pre>'; //debug line
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/39472-loop-problem/#findComment-190433
Share on other sites

<?php
    $query = "SELECT `mobile` FROM `customer`";
    $result = mysql_query($query) or die(mysql_error());
    $to = array();
    while(list($mobile) = mysql_fetch_row($result)) { $to[] = $mobile; }

    // Test it
    foreach($to as $mobile) { echo $mobile . "<br>"; }
?>

Link to comment
https://forums.phpfreaks.com/topic/39472-loop-problem/#findComment-190437
Share on other sites

When I use the code below (sorry I have included more of it this time) I get an error saying send message failed. Any ideas? new to programming as u might have guessed.

 

 

$query = "SELECT `mobile` FROM `customer`";
    $result = mysql_query($query) or die(mysql_error());
    $to = array();
    while(list($mobile) = mysql_fetch_row($result)) { $to[] = $mobile; }

    // Test it
    foreach($to as $mobile) { echo $mobile . "<br>"; }





$user = "xxxx";
$password = "xxxx";
$api_id = "xxxx";
$baseurl ="http://api.clickatell.com";
$text = $_POST['text'];
$to = array();
// auth call
$url = "$baseurl/http/auth?user=$user&password=$password&api_id=$api_id";
// do auth call
$ret = file($url);
// split our response. return string is on first line of the data returned
$sess = split(":",$ret[0]);
if ($sess[0] == "OK") {
$sess_id = trim($sess[1]); // remove any whitespace
$url = "$baseurl/http/sendmsg?session_id=$sess_id&to=$to&text=$text";
// do sendmsg call
$ret = file($url);
$send = split(":",$ret[0]);
if ($send[0] == "ID")
echo "success<br>message ID: ". $send[1];
else
echo "send message failed";
} else {
echo "Authentication failure: ". $ret[0];
exit();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/39472-loop-problem/#findComment-190460
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.