简体   繁体   English

Firebase 数据库删除具体数据 php

[英]Firebase database delete specific data by php

How can I remove firebase specific data?如何删除 firebase 特定数据? I use the php Kreait\Firebase library.我使用 php Kreait\Firebase库。

  $fg = $database->getReference('raw_check_out')->orderByChild('reciptno')->equalTo($recipt)->getSnapshot();

  $reb = $fg->getValue();
  $fg->remove();

but this is not working.但这不起作用。

Based on your code example: 根据您的代码示例:

$fg = $database
    ->getReference('raw_check_out')
    ->orderByChild('reciptno')
    ->equalTo($recipt)
    ->getSnapshot();

Here $fg does not hold the reference, but the snapshot. 这里$fg不保存引用,而是快照。

If you want to remove the reference after you have retrieved the data you need, you need the reference itself: 如果要在检索所需的数据后删除引用,则需要引用本身:

$fg = $database->getReference('raw_check_out');

$query = $fg->orderByChild('reciptno')->equalTo($recipt);
$reb = $query->getSnapshot()->getValue();

$fg->remove();

This function is for unsubscribing users if you want to remove "user-id-8776" and your structure is like: 如果您要删除“ user-id-8776”并且您的结构类似于:,此功能用于取消订阅用户。

在此处输入图片说明

public function unsubscribe($uid)
 {
    $ref = $this->database->getReference()->getChild('users')->getChild($uid)->orderByChild($uid)->getReference();
    $path = $ref->getUri()->getPath();
    $ref->remove();
 }

For any other data is more or less the same, you just have to find the reference of data you want to delete it and then you can remove it. 对于任何其他数据或多或少都是相同的,您只需要找到要删除的数据的引用,然后就可以删除它。

Check Image检查图像

在此处输入图像描述

You have another option to resolve this problem.您有另一种选择来解决此问题。 This solution given below:该解决方案如下:

    $url = "https://<projectID>.firebaseio.com/chatList/".<jsonFileName>."json";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
    $result = curl_exec($ch);
    curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM