2016-06-25 44 views
0

我有以下php腳本查詢mysql數據庫以發送消息。我想發送每1000行數據庫表循環中的「消息」變量。下面是代碼,有人可以找到錯誤嗎? php腳本本身不會產生任何錯誤,但是,它不會每1000行發送一條消息。相反,它會完全執行數據庫表的所有行,導致GCM發生消息傳遞錯誤。php不能通過sql用戶正確循環

<?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 
} 
} 

?> 

回答

0

$row[]總是附加,但$行不會被清零。

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

Thnx爲您的答案,我試過了,但腳本仍然一次發送到所有行。 GCM返回消息:批量消息數(1784)超過允許的最大值(1000),所以我只能推測消息一次發送到表的所有行。 – qwertyg