[英]php - Cannot use object of type PDOStatement as array with Google Cloud Messaging
I recently started using PDO to write my applications and i'm currently facing an issue where i'm pulling device ID's from a table and send it over to firebase for push notification purposes. 我最近开始使用PDO编写应用程序,目前正面临一个问题,即我要从表中提取设备ID并将其发送到Firebase进行推送通知。 With my below php script when i execute on the page i get this error
当我在页面上执行时,使用下面的PHP脚本,我收到此错误
Error 错误
Cannot use object of type PDOStatement as array in /Applications/XAMPP/xamppfiles/htdocs/myapp/send_fcm.php
PHP 的PHP
$row = $conn->query("SELECT device_id,os_type from devices");
while($row->setFetchMode(PDO::FETCH_ASSOC)) {
$gcmRegIds = array();
array_push($gcmRegIds, $row['device_id']);
echo json_encode($gcmRegIds);
}
$url = "https://fcm.googleapis.com/fcm/send";
$token = $gcmRegIds;
$serverKey = 'API_KEY';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'sound' =>'default');
$arrayToSend = array('to' => $token, 'notification' =>$notification,'priority'=>'high','badge'=>'1');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
PDO::query
returns a PDOStatement object for successful SELECT queries. PDO::query
为成功的SELECT PDO::query
返回一个PDOStatement对象。 Let's change the name of that variable, because $row
is a bit misleading there. 让我们更改该变量的名称,因为
$row
在这里有点误导。
$stmt = $conn->query("SELECT device_id,os_type from devices");
Now when you fetch from it, you can get rows. 现在,当您从中获取数据时,您可以获取行。
setFetchMode()
doesn't fetch a row, so we'll just use fetch()
with the PDO::FETCH_ASSOC
constant defining the fetch mode there. setFetchMode()
不会获取行,因此我们仅将fetch()
与PDO::FETCH_ASSOC
常量一起定义在那里的获取模式。
Assuming you want all the device_id values in an array, you need to move the array initialization ( $gcmRegIds = array();
) before the loop, or you'll set it back to an empty array before each value is pushed into it and end up with only the last value. 假设您希望将所有device_id值放入数组中,则需要在循环之前将数组初始化(
$gcmRegIds = array();
)移动,或者将其设置回空数组,然后再将每个值推入其中,并最后只有最后一个值。 Also, the array_push
function isn't really necessary to append one item to an array. 另外,将数组的一个项追加到数组中并不是必须要使用
array_push
函数。 You can just use []
. 您可以只使用
[]
。
$gcmRegIds = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$gcmRegIds[] = $row['device_id'];
}
And don't json_encode
inside the loop, do it afterward, or you'll have repeated, invalid JSON. 并且不要在循环内进行
json_encode
,事后再做,否则您将重复出现无效的JSON。
echo json_encode($gcmRegIds);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.