繁体   English   中英

php无法通过sql用户正确循环

[英]php does not loop correctly through sql users

我有以下查询mysql数据库以发送消息的php脚本。 我想在数据库表的每1000行中循环发送“ message”变量。 下面是代码,有人可以找到错误吗? php脚本本身不会产生任何错误,但是,它不会每隔1000行发送一次消息。 相反,它完全在数据库表的所有行上执行,从而导致来自GCM的消息传递错误。

<?php
 session_start();
 include_once 'connect.php';

  function send_notification($con,$registatoin_ids, $message) { 
    $url = 'https://android.googleapis.com/gcm/send';

    // prep the bundle
  $msg = array
  (
'message'   => "message",
'title'     => 'title',
'subtitle'  => 'subtitle. subtitle',
'msgcnt'    => 3,
'vibrate'   => 1,
'sound'     => 'default',
'soundname' => 'beep.wav',
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
  );
  $fields = array
  (
'registration_ids'  => $registatoin_ids,
'data'          => $msg
    );


    $headers = array(
        'Authorization: key=' . apiKey,
        'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
     curl_close($ch);
     echo $result;

 }

  if(isset($_SESSION['login_user'])){
   if(isset($_POST["submit"]) && isset($_POST["message"])) {    
    $num=$con->query("SELECT gcm_regid from gcm_users")->rowCount();
    $current_num=0;
    $message=$_POST["message"];
    for($i=0;$i<$num/999;$i++){

    $query=$con->query("SELECT gcm_regid from gcm_users LIMIT          
    $current_num,999");

foreach($query as $data) {
$row[]=$data["gcm_regid"];
}

   $pushStatus = send_notification($con,$row, $message);
   $current_num+=999;
   }
   }else if(isset($_POST["logout"])){

   if(session_destroy()) // Destroying All Sessions
 {
header("Location: login.php"); // Redirecting To Home Page
 }
 }

 ?>

$row[]总是追加,但是$ row从未清除。

$row[]=array(); // clear array
foreach($query as $data) {
$row[]=$data["gcm_regid"];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM