我创建函数以通过 PHP 代码从 mongoDB 获取数据。但它似乎效果不佳。
让我们看看我的代码
function updateperformancescore($timepf)
{
$mon = new Mongo('mongodb://127.0.0.1:27017');
$dbs = $mon->selectDB('bitdindatabase');
$col = $dbs->selectCollection('bd_performance_score');
$query2 = array("user_ID"=>"999");
echo "<br>query__".$query2["user_ID"];
$data2 = $col->find($query2,true);
echo "<br>count___".count($data2)."<br>";
echo "check is_object--".is_object($data2)."<br>";
//echo "--".$data2["user_ID"]."<br>";
error_reporting(E_ALL ^ E_NOTICE);
foreach ($data2 as $obj)
{
echo "aaaa";
$which = array("user_ID"=>$obj["user_ID"],"time"=>$obj["ps_avgtime"],"slug"=>$obj["lesson_slug"]);
echo json_encode($which);
}
和网页只显示
query_999计数__1 检查 is_object--1
但我在 mongo 中的数据是
{ "_id" : ObjectId("501e768eefbdb8637e2b00c8"), "user_ID" : 999, "lesson_slug" : 999, "ps_avgtime" : 999 }
{ "_id" : ObjectId("501e7698efbdb8637e2b00c9"), "user_ID" : 999, "lesson_slug" : 998, "ps_avgtime" : 888 }
{ "_id" : ObjectId("501e76a1efbdb8637e2b00ca"), "user_ID" : 999, "lesson_slug" : 997, "ps_avgtime" : 777 }
{ "_id" : ObjectId("501e76a9efbdb8637e2b00cb"), "user_ID" : 999, "lesson_slug" : 996, "ps_avgtime" : 77 }
{ "_id" : ObjectId("501e76b6efbdb8637e2b00cc"), "user_ID" : 999, "lesson_slug" : 995, "ps_avgtime" : 555 }
{ "_id" : ObjectId("501e76c0efbdb8637e2b00cd"), "user_ID" : 999, "lesson_slug" : 994, "ps_avgtime" : 444 }
{ "_id" : ObjectId("501e76caefbdb8637e2b00ce"), "user_ID" : 999, "lesson_slug" : 993, "ps_avgtime" : 333 }
{ "_id" : ObjectId("501e76d3efbdb8637e2b00cf"), "user_ID" : 999, "lesson_slug" : 992, "ps_avgtime" : 222 }
{ "_id" : ObjectId("501e76dcefbdb8637e2b00d0"), "user_ID" : 999, "lesson_slug" : 991, "ps_avgtime" : 111 }
{ "_id" : ObjectId("501e76e6efbdb8637e2b00d1"), "user_ID" : 999, "lesson_slug" : 990, "ps_avgtime" : 1 }
为什么它不进入 foreach ?以及此代码有什么问题以及如何正确获取数据。请帮助或指导我:)
编辑
我只是 error_log 它显示
致命错误:在 /var/www/web/sendanswer.php:92 中未捕获的异常“MongoException”和消息“MongoCursor 对象未由其构造函数正确初始化”堆栈跟踪:#0 /var/www/web/sendanswer。 php(92): MongoCursor->rewind() #1 /var/www/web/sendanswer.php(343): sendanswer->updateperformancescore(4.4269230365753) #2 {main} 下一个异常 'MongoException' 带有消息 'MongoCursor 对象/var/www/web/sendanswer.php:92 中的构造函数未正确初始化堆栈跟踪:#0 /var/www/web/sendanswer.php(92): MongoCursor->rewind() #1 / var/www/web/sendanswer.php(343): sendanswer->updateperformancescore(4.4269230365753) #2 {main} 在第 92 行的 /var/www/web/sendanswer.php 中抛出
这是什么意思 ??
编辑
这意味着“可能您的 Mongo 对象未连接到数据库服务器。”