Php 循环每行内容

Php 循环每行内容,php,android,push-notification,Php,Android,Push Notification,我使用这个脚本向单个用户发送推送通知 我想在脚本中做一个循环,将通知发送给所有用户 我必须从数据库中获取令牌,并为每个令牌循环 <?php require "init.php"; $message=$_POST['message']; $title=$_POST['title']; $path_to_fcm='https://fcm.googleapis.com/fcm/send'; $server_key="MY_SERVER_KEY"; $sql="select * from fcm

我使用这个脚本向单个用户发送推送通知

我想在脚本中做一个循环,将通知发送给所有用户

我必须从数据库中获取令牌,并为每个令牌循环

<?php

require "init.php";
$message=$_POST['message'];
$title=$_POST['title'];
$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="MY_SERVER_KEY";
$sql="select * from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_row($result);
$key=$row[0];

$headers= array(
'Authorization:key='.$server_key,
'Content-Type:application/json'
);

$fields= array('to'=>$key,
'notification'=>array('title'=>$title,'body'=>$message));

$payload=json_encode($fields);

$curl_session= curl_init();
curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session,CURLOPT_POST, true);
curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload);

$result=curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($con);?>
我改变了:

$key to $d[0];
但它不起作用

有什么建议吗

//更新

我只得到一个令牌,但数据库中有两个令牌 MySQLi错误 检查它:

//里格斯:这是我的更新:

我曾经测试过你的代码,没有出现错误

我添加了echo$row['fcm_token'];在while循环之后,但是页面没有显示我的回音

通知已发送到数据库中fcm_令牌的第一行。我认为Riggs循环没有获取第二行,也没有为新令牌设置$key

你能有一个简单的方法来做:

获取第一行令牌值 将$key设置为令牌 柜台++ 获取第二行令牌值 //执行相同的步骤

我猜当使用SELECT时,fcm_标记不是查询结果中返回的第一列*

当您使用*查询数据库时,即返回所有列,没有理由将所需的单个列作为结果数组中返回的第一列。因此,假设使用$row[0]

如果只希望返回特定的列,则在查询中指定列名,这样不仅可以知道哪一列将是第一列/第二列/。。。在结果数组中,它也更有效

$sql="select fcm_token from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_row($result);
// now using columns positions i.e. $row[0] will be getting the fcm_token col
$key=$row[0];
最好还是使用mysqli\u fetch\u assoc和列的名称,如

$sql="select fcm_token from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_assoc($result);
$key=$row['fcm_token'];
当循环返回的多个结果时,再次使用列名并使用参数MYSQLI_ASSOC,以便将行作为关联数组返回,即列名用作行数组的键

while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
    echo $row['fcm_token]; 
}

RE:你的评论,这是你建议你要做的吗

<?php
require "init.php";
$message=$_POST['message'];
$title=$_POST['title'];
$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="MY_SERVER_KEY";
$sql="select * from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);

// remove this it is fetching the first row outside the loop
//$row=mysqli_fetch_row($result);
//$key=$row[0];

$headers= array(
                'Authorization:key='.$server_key,
                'Content-Type:application/json'
        );


while($row=mysqli_fetch_assoc($result)) {

    $fields = array('to'=>$row['fcm_token'],
                   'notification'=>array(
                            'title'=>$title,
                            'body'=>$message
                            )
                    );

    $payload = json_encode($fields);

    $curl_session= curl_init();
    curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm);
    curl_setopt($curl_session,CURLOPT_POST, true);
    curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl_session,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_session,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload);

    $result=curl_exec($curl_session);
    curl_close($curl_session);
}

你用什么来形容它不起作用-你有错误?显示消息。。你有错误的结果。然后显示数据示例您的结果和预期结果,但它不起作用-定义它;将mysqli_error$con添加到查询中是否有任何错误?关于错误报告呢?抱歉,编辑了问题,代码起作用了,但它将通知发送到我从数据库获得的唯一令牌,但我有两个,可能是错误的循环?非常感谢RiggsFolly,但我想要的是,让我们假设我有多个令牌,一个是:1234,另一个是:5678,另一个。。我想将每个令牌设置为$key并发送通知。但在你的例子中,我仍然得到一个令牌。我们不能使用for循环吗?请参阅完整代码的编辑。您忘了在选择之后删除提取,这是提取第一个结果,而不处理它。现在循环正在处理所有结果,它应该对所有结果都有效。非常感谢@RiggsFolly,我很快就会看到它
while($row=mysqli_fetch_assoc($result))
{
    echo $row['fcm_token]; 
}
<?php
require "init.php";
$message=$_POST['message'];
$title=$_POST['title'];
$path_to_fcm='https://fcm.googleapis.com/fcm/send';
$server_key="MY_SERVER_KEY";
$sql="select * from fcm_info"; //there's one field called fcm_token
$result=mysqli_query($con,$sql);

// remove this it is fetching the first row outside the loop
//$row=mysqli_fetch_row($result);
//$key=$row[0];

$headers= array(
                'Authorization:key='.$server_key,
                'Content-Type:application/json'
        );


while($row=mysqli_fetch_assoc($result)) {

    $fields = array('to'=>$row['fcm_token'],
                   'notification'=>array(
                            'title'=>$title,
                            'body'=>$message
                            )
                    );

    $payload = json_encode($fields);

    $curl_session= curl_init();
    curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm);
    curl_setopt($curl_session,CURLOPT_POST, true);
    curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl_session,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_session,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload);

    $result=curl_exec($curl_session);
    curl_close($curl_session);
}