0

I want to use if condition with find query I'm not able to do so can anyone suggest me. I'm using php with mongo database-

 <?php
   $realtime = date("2016-09-22 12:55:24");
   $mongotime = New DateTime($realtime);
   $mt = $mongotime->getTimeStamp(); 
   var_dump($mt); 
   //database Connection
   $server= mongodb://localhost:27017"; 
   $c = new Mongo($server); 
   $db = $c->dbname; 
   echo "Database selected"; 

   //collection selection
   $collection = $db->collection; 
   echo "Collection selected"; 
   $cursor = $collection->find(); 
   //If Condition
   if (($realtime-timestamp)<=5) { 
     if (channel<=11) {
       if (channel>=36) { 
         echo “dual band”; 
       }
     } 
     if (channel>=36) { 
       if (channel<=11) {
         echo “dual band”; 
       }
     } else { 
       echo “Single band”;
     }
     foreach ($cursor as $doc) { 
       var_dump($doc); 
     }
   }
 ?>
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
Aps
  • 27
  • 11
  • 2
    Where is $channel / channel even set...? You cannot use `if ( channel )` in php, you will have to use `if ( $channel )` i.e a dollar sign to prefix variable names. – Stuart Nov 23 '16 at 17:23
  • channel is column in my database i need to check if the no of channel is less than 11 or greater than 36. i'm using $ with the values are user defined. – Aps Nov 24 '16 at 03:27

2 Answers2

0

I think there is a simple set of actions you can take to figure out what's going on here.

Debug your PHP

meanwhile, try this

ini_set('display_errors', 1);
error_reporting(E_ALL);

$mt = strtotime("2016-09-22 12:55:24"); 
var_dump($mt);

//database Connection
$server= mongodb://localhost:27017"; 
$c = new Mongo($server); 
$db = $c->dbname; 
echo "Database selected"; 

//collection selection
$collection = $db->collection; 
echo "Collection selected"; 
$cursor = $collection->find(); 

if (empty($cursor)) die('cursor is empty, we found nothing :(');
// hmm, what did we find?  what is it's structure?
var_dump($cursor);


// Loop over cursor in collection that you found
foreach ($cursor as $doc) {

  //If Condition where you are focused on $doc (current member of cursor)
  if (($mt - $doc['timestamp'])<=5) { 
    if ($doc['channel'] <= 11) {
      if ($doc['channel'] >= 36) { 
        echo “dual band”; 
      }
    } 
    if ($doc['channel'] >= 36) { 
      if ($doc['channel'] <= 11) {
        echo “dual band”; 
      }
    } else { 
      echo “Single band”;
    }
  }
}
Community
  • 1
  • 1
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • timestamp is a colum in database. So what i'm doing here is i'm putting a data & converting it in unix format then comparing this date with the date i have in database if the difference is less than or equal to 5 then i'm checking the channel of that data. So the part where i'm selecting collection it is working properly without if condition also i'm able to fetch the data but when using if condition that is not working properly. – Aps Nov 24 '16 at 03:39
0

In php we use generally fetch_array() in query E.g Select * table Id=1
Then result will id=1 and their values. Above mentioned in your code Query DB connection and Database connection is looks like php active DB manipulation used codeigniter php framework.Need to take result of the query then put as you mentioned above.

Vinoth Smart
  • 383
  • 3
  • 13
  • I'm connecting with mongo db So in mongo at the place of select query we need to use find. Till the connection part & collection selection it is working properly. I'm able to fetch the whole or particular part of database but when i'm using if condition i'm not able to fetch. – Aps Nov 24 '16 at 03:30