我如何使用if条件查找查询

时间:2016-11-23 17:21:08

标签: php mongodb

我想使用if find with find query我无法这样做可以有人建议我。我正在使用php和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); 
     }
   }
 ?>

2 个答案:

答案 0 :(得分:0)

我认为你可以采取一系列简单的行动来弄清楚这里发生了什么。

Debug your PHP

同时,试试这个

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”;
    }
  }
}

答案 1 :(得分:0)

在php中我们通常在查询E.g中使用fetch_array()选择* table Id = 1
然后结果将id = 1及其值。上面提到你的代码查询数据库连接和数据库连接看起来像php主动数据库操作使用codeigniter php框架。需要获取查询结果然后按上面提到的那样。