I'm trying to display the database 'hospitaldb', collection 'doctor' content using php. I've fields email, password, fname, lname in the collection. When I try to display these fields then I get error messages as :
Notice: Undefined index: email in C:\xampp\htdocs\phpmongodb\viewdoctor.php on line 40
Notice: Undefined index: password in C:\xampp\htdocs\phpmongodb\viewdoctor.php on line 41
Notice: Undefined index: fname in C:\xampp\htdocs\phpmongodb\viewdoctor.php on line 42
Notice: Undefined index: lname in C:\xampp\htdocs\phpmongodb\viewdoctor.php on line 43
$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";
}
Output :
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"
}
}