gpt4 book ai didi

php - mongodb getLastError() php

转载 作者:可可西里 更新时间:2023-11-01 10:44:30 25 4
gpt4 key购买 nike

我如何在 php 中使用 getLastError() 检查我的保存方法是否正在插入到 mongo?

我按如下方式设置我的数据库:

$this->databaseEngine = $app['mongo'];
$this->db = $this->databaseEngine->dbname;
$this->collection = $this->db->collectionname;

我的插入查询看起来像这样:

$query = array(
'fieldname' => $fieldname
);
$this->collection->insert($query);

然后我想使用 getLastError() 检查它是否正确插入,如果不正确,为什么。但我不确定如何实现。

我在插入后使用吗:

$this->collection->getLastError("what goes here?");

干杯。

更新

我最终用它来得到最后一个错误:

echo '<pre>' . print_r($this->databaseEngine->lastError(), true) . '</pre>';

Sammaye 的方法同样有效,见下文。

最佳答案

$this->collection->getLastError("what goes here?");

那里什么都没有,getLastError 的返回是来自 MongoDB 的最后一个错误 (http://www.php.net/manual/en/mongodb.lasterror.php)。它还用于 MongoDB 类 (atm)。

你不必那样使用它,相反你可以这样做:

$this->collection->insert($query, ARRAY('safe' => TRUE));

这将从函数返回一个数组,详细说明它是否实际插入。阵列的详细信息可以通过阅读此页面找到:

http://www.php.net/manual/en/mongocollection.insert.php

关于php - mongodb getLastError() php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13605385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com