2016-05-14 111 views
-1

我有以下php腳本查詢mysql數據庫以發送消息。我想發送每1000行數據庫表循環中的「消息」變量。下面是代碼,這有什麼問題?Php查詢for循環不能按預期工作

<?php 
    $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; 
    } 
?> 

https://jsfiddle.net/yLff8n5d/

+0

錯誤消息是什麼? – Fil

+0

那麼上面的代碼是怎麼回事? –

+0

錯誤是循環不會執行每1000行,因爲我想 – qwertyg

回答

1

檢查這個

<?php 
    $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"); 
     $row = array() 
     while($result = $query->fetch_assoc()){ 
      $row[] = $result['gcm_regid'] 
     } 
     $pushStatus = send_notification($con, $row, $message); 
     $current_num += 999; 
    } 
?> 
+0

所以我將不得不像下面的小提琴例子那樣整合一個while循環?你可以請一個快速檢查嗎? http://jsfiddle.net/yLff8n5d/1 – qwertyg

+0

我評論以下 –

+0

致命錯誤:調用未定義的方法PDOStatement :: fetch_assoc()。我也試過fetch_assoc(PDO :: FETCH_ASSOC),同樣的錯誤。 – qwertyg