以下是我正在尝试制作的代码,我会将其用于我网站上的身份验证页面:
CTRL + I
我已经创建并填充了“用户”这个集合,当我把它放在终端时我得到了一个结果:
<?php
$m = new MongoClient();
$db = $m->myDB;
$collection = $db->users;
$cursor = $collection->find({"Login":"admin","Password":"password"});
echo "No Error";
?>
但是我在脚本中没有工作,即页面没有回应“No Error”
由于
P.S:
我在我的收藏中以这种方式插入数据:
db.users.find({"Login":"admin","Password":"password"})
答案 0 :(得分:1)
<?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);
找到更多信息
答案 1 :(得分:0)
您也可以通过这种方式从集合中检索数据 -
// 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";
}