2016-07-04 51 views
0

我有PHP代碼顯示MongoDB中收集數據到PHP tablle,當我運行它,它不顯示任何輸出,MongoDB的採集數據到PHP表問題

請善待足以解決這

在這裏我的代碼

<?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>"; 
?> 

回答

1

您使用as $doc代替as $row這裏。

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

將其更改爲您需要使用的變量,是$doc而不是$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堅持己見:

您也談到了兩行,你在<head></head>之內包含您的標記,並在</body>之前放置</html>

$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>"; 

語法/結構:

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

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

您還可以添加<!DOCTYPE html>作爲第一行。


腳註:

您還需要確保該列的字母情況。

在遍歷循環中的行時區分大小寫。

因此Namename將被認爲是不同的。

如果(上述任何或全部)仍然不適用於您,那麼您可能在某處出現錯誤。

  • 因此,檢查錯誤。
+0

感謝,做工精細,我注意到我的錯誤:) – Kavinda

+0

@Kavinda請檢查您的所有問題的答案,並嘗試以紀念這是correct.Thanks的答案。 –