Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MongoDB不添加记录_Php_Mongodb_Mongodb Php - Fatal编程技术网

Php MongoDB不添加记录

Php MongoDB不添加记录,php,mongodb,mongodb-php,Php,Mongodb,Mongodb Php,我一直在努力解决这个问题,但我似乎找不到问题所在。我正试图在我的mongo DB中添加一条php记录(它已经是settup了,我已经测试了示例代码以确保它能正常工作) 添加记录的代码如下所示: // add a record $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" ); $collection->insert($obj); 添加记录的我的代码: $obj = arra

我一直在努力解决这个问题,但我似乎找不到问题所在。我正试图在我的mongo DB中添加一条php记录(它已经是settup了,我已经测试了示例代码以确保它能正常工作)

添加记录的代码如下所示:

// add a record
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj);
添加记录的我的代码:

$obj = array($currentarray[0][$currentCol] => $currentarray[$currentRow][$currentCol]);
$collection->insert($obj);
如果我打印对象:

Array ( [President ] => George Washington ) Array ( [Wikipedia Entry] => http://en.wikipedia.org/wiki/George_Washington ) Array ( [Took office ] => 30/04/1789 ) Array ( [Left office ] => 4/03/1797 ) Array ( [Party ] => Independent ) Array ( [Portrait] => GeorgeWashington.jpg ) Array ( [Thumbnail] => thmb_GeorgeWashington.jpg ) Array ( [Home State] => Virginia )
但是,如果我搜索,比如说“肖像”,我会得到一个错误:

DB Notice: Undefined index: Portrait in /home/me/public_html/yes.php on line 74
额外代码:

// connect
$m = new Mongo();

// select a database
$db = $m->data;

// select a collection (analogous to a relational database's table)
$collection = $db->test;

do{
while(comparison statement){
$obj = array($currentarray[0][$currentCol] => $currentarray[$currentRow][$currentCol]);
$collection->insert($obj);
}
} while (comparison statement);

// find everything in the collection
$cursor = $collection->find();

// iterate through the results
foreach ($cursor as $obj) {
    echo $obj["Party "] . "\n";
}

所以,我已经解决了我的问题(至少现在是这样)。我的记录被添加到我的数据库(上一个问题的快速打印($obj)显示给我)。所以现在,我唯一的问题是正确地检索它们,因为这就是我的错误的来源

更新:
我试图检索它们时出错,因为我试图在所有数组中“描绘”。这就是为什么我从检索语句中得到了大约7个错误和1个实际答案。

记录在数据库中吗?您可以验证是否可以使用名为
mongo
的MongDB客户机进行详细说明?我对php和MongoDB非常陌生,我只能通过查看文档来适应。试着运行MongoDB shell:
path/to/MongoDB/bin/mongo
如果在同一台机器上运行并监听端口1527(我猜),它会连接到服务器。如果您的服务器不在同一台机器上或侦听另一个端口,则在启动shell时必须使用一些参数。现在,如果您有一个连接,您可以查询数据库中的数据。首先,您必须使用
use
-stmt选择一个数据库。然后键入
db.mycollection.find()
以获取所有条目的列表。写入
help
以获取更多语句列表。我不确定路径,因为它不在我的服务器上。还有其他解决此问题的方法吗?没有更多的代码。也许您可以向我们展示与保存和查询数据相关的全部代码。