MongoDB için xampp bağlayın

1 Cevap php

Ben bir xampp 1.7.3 örneği çalışan ve aynı makine üzerinde bir MongoDB 1.2.4 sunucu var.

Onları bağlamak istiyorum, bu yüzden temelde aşağıdaki edilmiştir this tutorial php.net, bir bağlantı görünüyor ama imleç her zaman boş. Ben ne eksik bilmiyorum.

İşte ben çalışıyorum kodudur. İmleç-> geçerli hep yanlış diyor. teşekkürler

<?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"].'...';
}

?>

komut satırı ile yapıyor mükemmel çalışır

use CDO
db.rep_consulta.find()
-- lot of data here

1 Cevap

Sorgu sonuçlarını yineleme zaman imleç ilerleyen değildir. Imleç ileri varlık değildir çünkü yukarıdaki kod çalıştıran sonsuz bir döngüye neden oluyor. Değiştirmeyi deneyin:

$item = $cursor->current();

karşı

$item = $cursor->getNext();

Şahsen ben bu sözdizimi tercihim:

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

Edit

Aşağıdaki kod benim için iyi çalışıyor. Eğer bunu deneyebilir miyim?

$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 karşıthe first item of the result. Thats why you are getting false. You have yet karşıadvance the cursor at that point in your code.