Filter Firebase database with Multiple Conditions PHP

Issue

From last few days, I’m unable to find the solution of this Condition of using AND parameter in the Query

Example SQL QUERY `"SELECT * FROM `NotesMe` WHERE `Uid`='randomUid' AND Pin='Yep'"`

So I was unable to filter my database in firebase with Multiple Conditions Eg. WHERE AND

I’m working with the Firbase PHP SDK v6

I tried to filter the data with the as per the firebase filter documentation but i was only able to filter with a single condition

$data = $database->getReference($ref)->orderByChild('Uid')->equalTo($uid)->getValue();

Above data Varible only returns the value in order to UID only

enter image description here

So I want filter that vaules with multiple Condition like Where UID AND PIN

Solution

So After searching a lot, I thought that should i make my own filter for this,

so what as I know that the below $data variable is returns an filtered array with single condition of OrderByChild

$data = $database->getReference($ref)->orderByChild('Uid')->equalTo($uid)->getValue();

So I decided ti filter that array again with a PHP array filter

$array = $data; // $data contains is already filtered array 
$filter_name ='Pin'; // another conditions
$filter_value ='yep';
$expected = array_filter($array, function($el) use ($filter_value, $filter_name) {
    return ($el[$filter_name] >= $filter_value);

});

and finally it works now I’m able to use multiple conditions in firebase databse too with PHP

$expected contains the new re-filtered array

Answered By – Salman Shaikh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published