dongmao3148 2015-07-14 06:55
浏览 85
已采纳

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化