0

I have php code for displaying MongoDB collection data to php tablle, when i run it it doesn't show any output,

please be kind enough to sort this

Here my code

<?php

        $m = new MongoClient();
        $db = $m->selectDB('MapData');
        $collection = new MongoCollection($db,'ETom4');

        $cursor = $collection->find();
        //echo "<html><head><body>";
        echo "<table>";
        foreach($cursor as $doc) {
             echo "<tr>";
                echo "<td>" . $row['Name'] . "</td>";
                echo "<td>" . $row['Marks'] . "</td>";
                echo "<td>" . $row['value'] . "</td>"; 
             echo "</tr>";
        }
        echo "<table>";
        //echo "</html></head></body>";
?>
0

1 Answer 1

1

You're using as $doc instead of as $row here.

    foreach($cursor as $doc) {
         echo "<tr>";
            echo "<td>" . $row['Name'] . "</td>";
            echo "<td>" . $row['Marks'] . "</td>";
            echo "<td>" . $row['value'] . "</td>"; 

Change it to the variable you need to use, being $doc and not $row:

    $m = new MongoClient();
    $db = $m->selectDB('MapData');
    $collection = new MongoCollection($db,'ETom4');

    $cursor = $collection->find();
    //echo "<html><head><body>";
    echo "<table>";
    foreach($cursor as $doc) {
         echo "<tr>";
            echo "<td>" . $doc['Name'] . "</td>";
            echo "<td>" . $doc['Marks'] . "</td>";
            echo "<td>" . $doc['value'] . "</td>"; 
         echo "</tr>";
    }
    echo "<table>";
    //echo "</html></head></body>";

HTML stickler:

You also commented out 2 lines which you're including your markup inside <head></head> and have placed </html> before </body>.

    $m = new MongoClient();
    $db = $m->selectDB('MapData');
    $collection = new MongoCollection($db,'ETom4');

    $cursor = $collection->find();
    //echo "<html><head></head><body>";
    echo "<table>";
    foreach($cursor as $doc) {
         echo "<tr>";
            echo "<td>" . $doc['Name'] . "</td>";
            echo "<td>" . $doc['Marks'] . "</td>";
            echo "<td>" . $doc['value'] . "</td>"; 
         echo "</tr>";
    }
    echo "<table>";
    //echo "</body></html>";

The syntax/structure is:

<html>
  <head>
  ...
  </head>

 <body>
 ...
 </body>
</html>

You can also add <!DOCTYPE html> as the first line.


Footnotes:

You also need to make sure of the columns' letter-case.

They are case-sensitive when iterating over rows in a loop.

Therefore Name and name would be considered different.

If (any or all of) the above still doesn't work for you, then you may have errors somewhere.

  • So, check for errors.
Sign up to request clarification or add additional context in comments.

1 Comment

@Kavinda please check your all questions answers and try to mark the answers which are correct.Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.