donglinxi1467 2010-03-12 20:54
浏览 119
已采纳

将xampp连接到MongoDB

I have a xampp 1.7.3 instance running and a MongoDB 1.2.4 server on the same machine.

I want to connect them, so I basically have been following this tutorial on php.net, it seems to connect but the cursors are always empty. I don't know what am I missing.

Here is the code I am trying. The cursor->valid always says false. thanks

<?php
$m = new Mongo(); // connect
try {
  $m->connect();
} catch (MongoConnectionException $ex) {
  echo $ex;
}
echo "conecta...";
$dbs = $m->listDBs();
if ($dbs == NULL) {
  echo $m->lastError();
  return;
}
foreach($dbs as $db) {
  echo $db;
}

$db = $m->selectDB("CDO");
echo "elige bd...";
$col = $db->selectCollection("rep_consulta");
echo "elige col...";

$rangeQuery = array('id' => array( '$gt' => 100));
$col->insert(array('id' => 456745764, 'nombre' => 'cosa'));
$cursor = $col->find()->limit(10);
echo "buscando...";
var_dump($cursor);
var_dump($cursor->valid());
if ($cursor == NULL) echo 'cursor null';
while($cursor->hasNext()) {
    $item = $cursor->current();
    echo "en while...";
    echo $item["nombre"].'...';
}

?>

doing this by command line works perfect

use CDO
db.rep_consulta.find()
-- lot of data here
  • 写回答

1条回答 默认 最新

  • doulei8861 2010-03-13 00:25
    关注

    When iterating the results of the query you are not advancing the cursor. Running your code above is causing an infinite loop since the cursor isn't being advanced. Try changing :

    $item = $cursor->current();
    

    to

    $item = $cursor->getNext();
    

    Personally, I prefer this syntax :

    foreach ($cursor as $item)
    {
        var_dump($item);
    }
    

    Edit

    The following code is working fine for me. Can you try it out?

    $m = new Mongo();
    
    $db = $m->CDO;
    $col = $db->rep_consulta;
    
    $col->insert(array('id' => 456745764, 'nombre' => 'cosa'));
    
    $cursor = $col->find()->limit(10);
    
    foreach ($cursor as $item)
    {
        var_dump($item);
    }
    

    Edit++

    By the way, $cursor->valid() will not return true until you advance the cursor to the first item of the result. Thats why you are getting false. You have yet to advance the cursor at that point in your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度