我试图用htnl显示mongodb中的数据,但是我编写的代码没有做到这一点,我也不知道应该更改什么,这样它才能正常工作。

<body>
<?php
    $server = "mongodb://localhost:27017/test";
   // connect to mongodb
   $m = new MongoClient($server);
   // select a database
   $db = $m->test;
?>

    try{
        // Connecting to server
    $c = new MongoClient( $server );
    }catch(MongoConnectionException $connectionException){
        print $connectionException;
        exit;
    }

    $data  = "<table style='border:1px solid red;";
    $data .= "border-collapse:collapse' border='1px'>";
    $data .= "<thead>";
    $data .= "<tr>";
    $data .= "<th>Name</th>";
    $data .= "<th>UserID</th>";
    $data .= "<th>Email</th>";
    $data .= "<th>Sites Links</th>";
    $data .= "<th>Imgaes</th>"
    $data .= "</tr>";
    $data .= "</thead>";
    $data .= "<tbody>";

    try{
        $collection = $db->links;
        $people = $collection->find();
        foreach($people as $document){
            $data .= "<tr>";
            $data .= "<td>" . $document["Name_of_the_person"] . "</td>"; // Name
            $data .= "<td>" . $document["userID"]."</td>"; // UserID
            $data .= "<td>" . $document["email"]."</td>"; // Email
            $data .= "<td>" . $document["links"]."</td>"; // Sites
            $data .= "<td>" . $document["imagesurl"]."</td>"; // Images
            $data .= "</tr>";
        }
        $data .= "</tbody>";
        $data .= "</table>";
        echo $data;

    }catch(MongoException $mongoException){
        print $mongoException;
        exit;
    }

</body>
</html>

我真的很想知道代码出了什么问题。
谢谢

最佳答案

我同意vmr的评论。但是如果你还想使用代码:

<?php
$server = "mongodb://localhost:27017/test";
// connect to mongodb
$m = new MongoClient($server);
// select a database
$db = $m->test;

?>

<table style='border:1px solid red border-collapse:collapse' border='1px'>";
    <thead>
        <tr>
            <th>Name</th>
            <th>UserID</th>
            <th>Email</th>
            <th>Sites Links</th>
            <th>Images</th>
        </tr>
    </thead>
<tbody>


<?php
try{
    $collection = $db->links;
    $people = $collection->find();
    foreach($people as $document){
       echo "<tr>";
       echo "<td>".$document["Name_of_the_person"]."</td>"; // Name
       echo "<td>".$document["userID"]."</td>"; // UserID
       echo "<td>".$document["email"]."</td>"; // Email
       echo "<td>".$document["links"]."</td>"; // Sites
       echo "<td>".$document["imagesurl"]."</td>"; // Images
       echo "</tr>";
   }
    echo "</tbody>";
    echo "</table>";
}catch(MongoException $mongoException){
   print $mongoException;
   exit;
}
?>

我知道这个职位很老了。但如果你还在寻找解决办法,也许会有帮助。

关于php - 使用HTML文件显示MongoDB集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26716035/

10-09 08:28