繁体   English   中英

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

[英]How can i use if condition with find query

我想使用条件条件与查找查询我无法这样做,任何人都可以建议我。 我在mongo数据库中使用php-

 <?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); 
     }
   }
 ?>

我认为您可以采取一系列简单的措施来弄清这里发生的事情。

调试您的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”;
    }
  }
}

在php中,我们通常在查询Eg Select * table Id = 1中使用fetch_array()
然后结果将为id = 1及其值。 上面提到的在您的代码中查询数据库连接和数据库连接看起来像是使用php主动数据库操作的codeigniter php框架。需要获取查询结果然后如上所述。

暂无
暂无

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

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