我正在尝试显示数据库'hospitaldb',使用php收集'doctor'内容。我在集合中输入了电子邮件,密码,fname,lname字段。当我尝试显示这些字段时,我收到以下错误消息:
注意:未定义索引:第40行中C:\ xampp \ htdocs \ phpmongodb \ viewdoctor.php中的电子邮件
注意:未定义的索引:第41行的C:\ xampp \ htdocs \ phpmongodb \ viewdoctor.php中的密码
注意:未定义的索引:第42行的C:\ xampp \ htdocs \ phpmongodb \ viewdoctor.php中的fname
注意:未定义索引:第43行的C:\ xampp \ htdocs \ phpmongodb \ viewdoctor.php中的lname
$server = "mongodb://localhost:27017/hospitaldb";
try{
$connection = new MongoDB\Client($server);
/*$hospitaldb = $connection->test;*/
$hospitaldb = $connection->hospitaldb;
}catch (MongoConnectionException $ConErrMsg)
{
echo "$ConErrMsg";
}
$data = "<table style='border:1px solid red;";
$data .= "border-collapse:collapse' border='1px'>";
$data .= "<thead>";
$data .= "<tr>";
$data .= "<th>First Name</th>";
$data .= "<th>Last Name</th>";
$data .= "<th>Email</th>";
$data .= "<th>Password</th>";
$data .= "</tr>";
$data .= "</thead>";
$data .= "<tbody>";
try{
$db = $connection->hospitaldb;
$collection = $db->doctors;
$cursor = $collection->find();
foreach ($cursor as $document)
{
$data .= "<tr>";
$data .= "<td>" . $document["email"] . "</td>";
$data .= "<td>" . $document["password"]."</td>";
$data .= "<td>" . $document["fname"]."</td>";
$data .= "<td>" . $document["lname"]."</td>";
$data .= "</tr>";
}
}catch (MongoException $mongoException)
{
echo "$mongoException";
}
输出:
object(MongoDB\Model\BSONDocument)#20 (1) {
["storage":"ArrayObject":private]=>
array(5) {
["_id"]=>
object(MongoDB\BSON\ObjectId)#16 (1) {
["oid"]=>
string(24) "5ca746b2a930602fb8000e04"
}
["email"]=>
string(16) "sanekt@gmail.com"
["password"]=>
string(6) "sanket"
["fname"]=>
string(6) "sanket"
["lname"]=>
string(6) "sanket"
}
}
object(MongoDB\Model\BSONDocument)#22 (1) {
["storage":"ArrayObject":private]=>
array(5) {
["_id"]=>
object(MongoDB\BSON\ObjectId)#21 (1) {
["oid"]=>
string(24) "5ca757fea930602fb8000e05"
}
["email"]=>
string(11) "p@gmail.com"
["password"]=>
string(4) "7888"
["fname"]=>
string(6) "piyush"
["lname"]=>
string(4) "shah"
}
}
object(MongoDB\Model\BSONDocument)#16 (1) {
["storage":"ArrayObject":private]=>
array(5) {
["_id"]=>
object(MongoDB\BSON\ObjectId)#20 (1) {
["oid"]=>
string(24) "5ca8444aa9306032cc001a32"
}
["email"]=>
string(17) "shahsir@gmail.com"
["password"]=>
string(7) "shahsir"
["fname"]=>
string(4) "Shah"
["lname"]=>
string(4) "Shah"
}
}
答案 0 :(得分:0)
您发布的问题对我有用:
我的设置: PHP 7.2线程安全 从此处https://pecl.php.net/package/mongodb/1.5.3/windows下载了用于Windows的DLL。我把它放在ext文件夹中。
Composer.json
"require": {
"mongodb/mongodb": "^1.4"
}
}
帖子中执行的php代码
<?php
require_once __DIR__ . "/vendor/autoload.php";
$server = "mongodb://localhost:27017/hospitaldb";
try{
$connection = new MongoDB\Client($server);
/*$hospitaldb = $connection->test;*/
$hospitaldb = $connection->hospitaldb;
}catch (MongoConnectionException $ConErrMsg)
{
echo "$ConErrMsg";
}
$data = "<table style='border:1px solid red;";
$data .= "border-collapse:collapse' border='1px'>";
$data .= "<thead>";
$data .= "<tr>";
$data .= "<th>First Name</th>";
$data .= "<th>Last Name</th>";
$data .= "<th>Email</th>";
$data .= "<th>Password</th>";
$data .= "</tr>";
$data .= "</thead>";
$data .= "<tbody>";
try{
$db = $connection->hospitaldb;
$collection = $db->doctors;
$cursor = $collection->find();
foreach ($cursor as $document)
{
$data .= "<tr>";
$data .= "<td>" . $document["first_name"] . "</td>";
$data .= "<td>" . $document["last_name"]."</td>";
$data .= "</tr>";
}
}catch (MongoException $mongoException)
{
echo "$mongoException";
}
var_dump($data);