直接访问 PHP 文件时,我没有从数据库返回任何结果,但是echo
下面的所有检查都返回了预期的结果。
这是整个 PHP 文件。
<?php
$ext = new ReflectionExtension('mongo'); // see http://php.net/manual/en/reflectionextension.getversion.php
var_dump($ext->getVersion()); // returns string(5) "1.3.4"
echo extension_loaded("mongo") ? "loaded<br />" : "not loaded<br />"; // check if driver installed - returns "loaded"
$dbhost = '127.x.xx.x';
$dbname = 'mydb';
$m = new Mongo("mongodb://$dbhost"); // also tried 'MongoClient' rather than 'Mongo'
if ($m == true) {
echo "<br />connected to {$m}<br />"; // returns "connected to 127.x.xx.x:27017
}
$db = $m->$dbname;
$collection = $db->myCollection;
echo "<br />my collection is called {$collection}<br />"; // returns "my collection is called mydb.myCollection"
$query = $collection->find();
if ($query == true) {
echo "<br />the find method works"; // this is being returned
}
foreach($query as $result) { // nothing is happening here
var_dump($result);
}
?>
命令行查询:
db.myCollection.find();
正在返回预期结果(有一个文档)。