foreach 循环仅适用于 PHP 中数据库的第一条记录


foreach loop working only for first record of database in php

我在代码中遇到了一个条件。我正在从 php 向多个用户发送 gcm 推送通知。我正在从数据库中获取注册 ID。在代码中,我使用 foreach 循环来发送通知。但我不确定为什么它只向一个用户发送推送通知,而不是向所有用户发送推送通知。如果我删除推送通知代码,那么它将向我显示数据库中的所有注册 ID。

<?php
$host_name = "localhost";
$user_name = "xxxxxxx";
$user_pass = "xxxxx";
$db_name = "xxxxxx";
$conn=mysql_connect($host_name,$user_name,$user_pass);
if(!$conn) {
    echo"could not connect:".mysql_error();
} else {
    mysql_select_db($db_name,$conn);
}
$query = "select reg_id from property_register where reg_id != ''";
$result = mysql_query($query) or die(mysql_error());
foreach(mysql_fetch_array($result) as $r_id) {
    $gcmRegID  =  $r_id[0];
    echo $gcmRegID . "<br>";
    $pushMessage = "New Property Posted in CPO";
    if (isset($gcmRegID) && isset($pushMessage)) {
        $gcmRegIds = array($gcmRegID);
        $message = array("m" => $pushMessage); 
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $gcmRegIds,
            'data' => $message,
        );
        // Update your Google Cloud Messaging API Key
        define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            '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_VERIFYHOST, 0);   
        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);
        return $result;
    }
}
?>

简而言之,foreach 循环只是将通知发送给第一个用户,而不是向所有用户发送通知。如果我删除 gcm 代码并执行 foreach 循环记录的回显,它会向我显示所有

谢谢

你的 foreach 循环中有一个返回,这将导致通过 foreach 的第一个遍历触发 retrun 调用,并且永远不会循环另一个 foreach 。不需要退货。请改用以下代码。

<?php
$host_name = "localhost";
$user_name = "xxxxxxx";
$user_pass = "xxxxx";
$db_name = "xxxxxx";
$conn=mysql_connect($host_name,$user_name,$user_pass);
if(!$conn) {
    echo"could not connect:".mysql_error();
} else {
    mysql_select_db($db_name,$conn);
}
$query = "select reg_id from property_register where reg_id != ''";
$result = mysql_query($query) or die(mysql_error());
foreach(mysql_fetch_array($result) as $r_id) {
    $gcmRegID  =  $r_id[0];
    echo $gcmRegID . "<br>";
    $pushMessage = "New Property Posted in CPO";
    if (isset($gcmRegID) && isset($pushMessage)) {
        $gcmRegIds = array($gcmRegID);
        $message = array("m" => $pushMessage); 
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $gcmRegIds,
            'data' => $message,
        );
        // Update your Google Cloud Messaging API Key
        define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            '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_VERIFYHOST, 0);   
        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);
    }
}
?>