gpt4 book ai didi

php - 循环每行内容

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:14 26 4
gpt4 key购买 nike

我正在使用此脚本向单个用户发送推送通知。

我想在脚本中循环以将通知发送给所有用户。

我必须从数据库中获取 token 并为每个 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);
$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);?>

我试过的是:

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

我改变了:

$key to $d[0];

但是没有成功,

有任何建议,请。

//更新了

  1. 我只得到一个 token ,但我在数据库中得到了两个
  2. MySQLi 错误

查看:Result

//Riggs 这是我的更新:

我曾经测试过您的代码,没有出现任何错误。

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

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

你有办法简单地做:

  1. 获取第一行的token值
  2. 将 $key 设置为 token
  3. 计数器++
  4. 获取第二行的token值

//做同样的步骤

最佳答案

我的猜测是,当使用 SELECT *

时, fcm_token 不是查询结果中返回的第一列

当您使用 * 查询数据库时,即返回所有列,您想要的单个列没有理由成为结果数组中返回的第一列。所以假设使用 $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_fetch_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];
}

或者

while($row=mysqli_fetch_assoc($result))
{
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);
}

关于php - 循环每行内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42790651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com