0

Ok i am tryna make this code excute for every phone number in my db. Before following advice here i was only able to send to one number at a time, i got advice to add a while statment to loop it. However my code don't wanna work no more. Here is before and after. I know i did something wrong.

BEFORE (Working-no db)

// Set SMS options
$data['post'] = array (
  '_rnr_se'     => $rnrse,
  'phoneNumber' => '123456789', 
  'text'        => 'This is a test SMS',
  'id'          => ''
);

// Send the SMS
$response = xcurl::fetch('api.gateway.com/', $data);

// Evaluate the response
$value = json_decode($response['data']);

if($value->ok) {
  echo "SMS message sent! ({$data['post']['phoneNumber']}: {$data['post']['text']})";    
} else {
  echo "Unable to send SMS! Error Code ({$value->data->code})\n\n";
  echo     'data: '; print_r(    $data); echo "\n\n";
  echo 'response: '; print_r($response);
}

AFTER(Not working with db)

// Set SMS options
mysql_connect("localhost", "music", "") or
    die("Could not connect: " . mysql_error()); mysql_select_db("music");

$result = mysql_query("SELECT id, numbers FROM account_templates");

while ($row = mysql_fetch_array($result)) {
  $data['post'] = array (
    '_rnr_se'     => $rnrse,
    'phoneNumber' => $row['numbers'], 
    'text'        => 'This is a test SMS',
    'id'          => '' 
  );

  // Send the SMS
  $response = xcurl::fetch('api.gateway.com', $data);

  // Evaluate the response
  $value = json_decode($response['data']);
}
if($value->ok) {
  echo "SMS message sent! ({$data['post']['phoneNumber']}: {$data['post']['text']})";
} else {
  echo "Unable to send SMS! Error Code ({$value->data->code})\n\n";
  echo     'data: '; print_r(    $data); echo "\n\n";
  echo 'response: '; print_r($response);
}
6
  • 1
    possible duplicate of Run the array mutiple times Jun 28, 2010 at 8:09
  • 1
    not really its a different problem, same script
    – Aaron
    Jun 28, 2010 at 8:11
  • try to fix it up the best i can, hope someone can help me!
    – Aaron
    Jun 28, 2010 at 8:27
  • There is a code button in the editor (the one with the ones and zeros).
    – jigfox
    Jun 28, 2010 at 8:31
  • thx to who ever fixed it
    – Aaron
    Jun 28, 2010 at 8:33

1 Answer 1

0

The way it is now, the sending process is running through the while loop, but the process of checking whether the SMS was sent is not.

It could be as simple as changing the position of a }.

Take away this bracket:

 // Evaluate the response
  $value = json_decode($response['data']);
} <------------------------------------------------ remove that one

and move it

     echo 'response: '; print_r($response);
   }
 } <------------------------------------------------add here
5
  • THANKS, do you have any idea how i can see and debug what is being posted, i know it posting correctly but i think i made a mistake with my sql.
    – Aaron
    Jun 28, 2010 at 8:47
  • @Pekka do you see any reason y i get No database selected
    – Aaron
    Jun 28, 2010 at 8:58
  • @Aaron you need to select one after connecting: php.net/mysql_select_db
    – Pekka
    Jun 28, 2010 at 9:00
  • @Pekka hate to keep bothering you, but do yu have any idea y it juss workin for the first number in the db?
    – Aaron
    Jun 28, 2010 at 9:16
  • @Aaron nope, sorry. If you moved the bracket as I wrote above, the loop should run through. Try checking mysql_num_rows() to see how many rows your query returns.
    – Pekka
    Jun 28, 2010 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.