Php 在mongoDB$collection中->;find()不';t工作
这是我正在努力实现的代码,我将在我的网站上的身份验证页面上使用它:Php 在mongoDB$collection中->;find()不';t工作,php,mongodb,Php,Mongodb,这是我正在努力实现的代码,我将在我的网站上的身份验证页面上使用它: <?php $m = new MongoClient(); $db = $m->myDB; $collection = $db->users; $cursor = $collection->find({"Login":"admin","Password":"password"}); echo "No Error"; ?> 但我不在脚本中工作,即页面不回显“无错误” 谢谢 p、
<?php
$m = new MongoClient();
$db = $m->myDB;
$collection = $db->users;
$cursor = $collection->find({"Login":"admin","Password":"password"});
echo "No Error";
?>
但我不在脚本中工作,即页面不回显“无错误”
谢谢
p、 s:
我以这种方式在我的集合中插入数据:
$m = new MongoClient();
$db = $m->myDB;
$collection = $db->users;
$document = array(
"Login" => "admin",
"Password" => "password"
);
$collection->insert($document);
您也可以通过这种方式从集合中检索数据-
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully" . "<br/>";
// select a database
$db = $m->mydb;
echo "Database mydb selected" . "<br/>";
// select a collection
$collection = $db->users;
echo "Collection selected succsessfully" . "<br/>";
// find all informations
$cursor = $collection->find();
// iterate cursor to display Login
foreach ($cursor as $info) {
echo $info["Login"] . "\n";
}
//连接到mongodb
$m=新的MongoClient();
回显“成功连接到数据库”。“
”;
//选择一个数据库
$db=$m->mydb;
回显“已选择数据库mydb”。“
”;
//选择一个集合
$collection=$db->users;
回显“已成功选择收藏”。“
”;
//查找所有信息
$cursor=$collection->find();
//迭代光标以显示登录
foreach($cursor作为$info){
echo$info[“登录”]。“\n”;
}
我建议您打开错误报告,$collection->find({“Login”:“admin”,“Password”:“Password”})
不是有效的PHP代码。@malarzm我受此链接启发,编写了我的代码:对我来说,它应该可以工作,这就是我在这里发表评论的原因。你能告诉我们如何将数据插入到集合中吗?它应该是key=>value
pair。@KhairulIslam我刚刚更新了我的帖子谢谢,但是如何在页面上显示find()返回的文档?
<?php
// MongoClient
$m = new MongoClient();
// Select your database
$db = $m->selectDB('database_name');
// MongoCollection, pasing $db and collection name
$collection = new MongoCollection($db, 'users');
// Your query
$query = array(
'Login' => 'admin',
'Password' => 'password'
);
$cursor = $collection->find($query);
var_dump($cursor);
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully" . "<br/>";
// select a database
$db = $m->mydb;
echo "Database mydb selected" . "<br/>";
// select a collection
$collection = $db->users;
echo "Collection selected succsessfully" . "<br/>";
// find all informations
$cursor = $collection->find();
// iterate cursor to display Login
foreach ($cursor as $info) {
echo $info["Login"] . "\n";
}