tags:

views:

65

answers:

1

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);
}
A: 

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
Pekka
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
@Aaron try `echo mysql_error();` after the `mysql_query()`
Pekka
@Pekka do you see any reason y i get No database selected
Aaron
@Aaron you need to select one after connecting: http://www.php.net/mysql_select_db
Pekka
@Pekka hate to keep bothering you, but do yu have any idea y it juss workin for the first number in the db?
Aaron
@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