dongmao3148
2015-07-14 06:55
浏览 50

Mongodb连接错误?

<?php
$username='root';
$password='xyz';
$database='abc';
$host='localhost';
function MongoConnect($username, $password, $database, $host) {
    $con = new Mongo("mongodb://{$username}:{$password}@{$host}"); // Connect to Mongo Server
    $db = $con->selectDB($database); // Connect to Database
}
$collection=$db->movie;
$document = array( "name" =>"Calvin and Hobbes");
$collection->insert($document);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $document) {
    echo $document["name"] . "
";
}
?>

I had installed MONGO DB and tried to test my DB, but I am getting an ERROR

"Internal Server Error 500"

And also my Test.php file have my own content called Hello World, but if I had run the TEST.php file it displays Nothing. My DB table is not accessing and I wasn't able to retrieve data from my Database. So Kindly help me out here.

图片转代码服务由CSDN问答提供 功能建议

 &lt;?php 
 $ username ='root'; 
 $ password ='xyz'  ; 
 $ database ='abc'; 
 $ host ='localhost'; 
 
函数MongoConnect($ username,$ password,$ database,$ host){
 $ con = new Mongo(“mongodb:// {  $用户名}:{$密码} @ {$主机}“);  //连接到Mongo Server 
 $ db = $ con&gt; selectDB($ database);  //连接到数据库
} 
 $ collection = $ db-&gt; movie; 
 $ document = array(“name”=&gt;“Calvin and Hobbes”); 
 $ collection-&gt; insert($ 文档); 
 //找到集合中的所有内容
 $ cursor = $ collection-&gt; find(); 
 //遍历结果
foreach($ cursor as $ document){
 echo $ document [  “名称”] 。  “
”; 
} 
?&gt; 
   
 
 

我安装了MONGO DB并试图测试我的数据库,但是我收到了错误

“内部服务器错误500”

还有我的 Test.php < / code> 文件有我自己的内容叫做 Hello World ,但是如果我运行了 TEST.php 文件,它就显示Nothing。 我的数据库表不是 访问,我无法从我的数据库中检索数据。 所以请在这里帮助我。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drt5813 2015-07-14 07:15
    已采纳

    There can be several things wrong.
    First - is Mongo driver installed?
    Second - your MongoConnect function have no effect. You are defining it and not calling. Plus even if you would call it there would be no effect as $db is only in function scope and not outside. Third - because function MongoConnect have no effect "$collection=$db->movie;" will result in problem as $db is not defined.
    Consult http://php.net/manual/en/mongocollection.insert.php on how to insert data in collection.

    打赏 评论
  • doutan3192 2015-07-14 07:13

    Internal Server Error only occured when misspelled in code or some of function called wrongly. Please review ur code.

    打赏 评论

相关推荐 更多相似问题