[英]Can't get data from mongoDB
我創建了通過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);
}
和網頁僅顯示
查詢_ 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堆棧跟蹤:#0 / var / www / web / sendanswer中未捕獲的異常“ MongoException”,消息為“ MongoCursor對象尚未由其構造函數正確初始化”。 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對象未連接到數據庫服務器。”
我看到的是數據類型錯誤。
在數據庫中,user_ID是整數,在PHP查詢中,它是一個字符串。 嘗試:
$query2 = array("user_ID"=> 999);
代替 :
$query2 = array("user_ID"=>"999");
PHP在數據類型上可能很容易,但是我不確定Mongodb及其PHP驅動程序。
您的find
語句中還有一個錯誤的參數。 刪除true
因為它應該是字段數組,而不是bool
這段代碼可以正常工作:
<?php
$mon = new Mongo('mongodb://127.0.0.1:27017');
$dbs = $mon->selectDB('my_db');
$col = $dbs->selectCollection('newColl');
$query2 = array("user_ID"=> 999);
echo "<br>query__".$query2["user_ID"];
$data2 = $col->find($query2);
error_reporting(E_ALL ^ E_NOTICE);
foreach ($data2 as $obj)
{
$which = array("user_ID"=>$obj["user_ID"],
"time"=>$obj["ps_avgtime"],
"slug"=>$obj["lesson_slug"]);
echo json_encode($which);
echo PHP_EOL ;
}
?>
輸出:
[......]$ php testmongo.php
<br>query__999{"user_ID":999,"time":999,"slug":999}
{"user_ID":999,"time":999,"slug":999}
{"user_ID":999,"time":339,"slug":999}
{"user_ID":999,"time":339,"slug":5599}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.