0

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);
            }

および Web ページの表示のみ

query_ 999 count __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. php(92): MongoCursor->rewind() #1 /var/www/web/sendanswer.php(343): sendanswer->updateperformancescore(4.4269230365753) #2 {main} 次の例外 'MongoException' とメッセージ 'The MongoCursor object /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 オブジェクトがデータベース サーバーに接続されていない可能性がある」ことを意味します。

4

1 に答える 1

1

私が見ているのは、データ型のエラーです。

データベースの 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}
于 2012-08-07T06:53:44.360 に答える