douhuan1937 2017-03-14 15:52 采纳率: 0%
浏览 36
已采纳

循环每行内容

I am having this script to send a push notification to a single user.

I want to make a loop in the script to send the notification to ALL users.

I must get the tokens from the database and loop for each one.

<?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);?>

What I have tried is:

while($d=mysqli_fetch_array($result,MYSQLI_BOTH))
{
echo $d[0]; 
}

and I changed:

$key to $d[0];

But it didn't work,

Any suggestions, please.

//Updated

  1. I am getting only one token but I got two in the database
  2. MySQLi error

Check it: Result

//Riggs is this my update:

I used to test your code and no error has shown up.

I added echo $row['fcm_token']; after the while loop but the page is not displaying my echo.

The notification has been sent to the first row of fcm_token in the database. I think Riggs that the loop isn't fetching the second row and it isn't setting the $key to the new token.

Can u have a way to simply do:

  1. Getting the first-row token value
  2. setting the $key to the token
  3. Counter ++
  4. Getting the second-row token value

//do same steps

  • 写回答

1条回答 默认 最新

  • duanji6997 2017-03-14 16:02
    关注

    My guess would be that fcm_token is not the first column returned in the query result, when using SELECT *

    When you query a database using the * i.e. return all columns there is no reason why the single column you want will be the first column returned in the result array. So it is dangerous to assume that by using $row[0]

    Instead specify the column name in the query if you only want specific columns returned, not only do you then know which column will be the first/second/... in the result array, it is also more efficient.

    $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];
    

    Better still use mysqli_fetch_assoc() and the name of the column like

    $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'];
    

    When it comes to your loop over the multiple results returned, again use a column name and use the parameter MYSQLI_ASSOC so the row gets returned as an associative array i.e. the column names are used as the key to the row array.

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

    Or

    while($row=mysqli_fetch_assoc($result))
    {
        echo $row['fcm_token]; 
    }
    

    RE: You comment, is this what you are suggesting you want to do

    <?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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害