Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8337779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:23:39+00:00 2026-06-09T04:23:39+00:00

I create function to get data from mongoDB by PHP code. But It seem

  • 0

I create function to get data from mongoDB by PHP code. But It seem not working well.

let’s see my code

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

and webpage show only

query_999
count
__1
check is_object–1

but my data in mongo is

{ "_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 }

Why it’s not going into foreach ? And What’s wrong with this code and How I get data correctly.
help or guide me please 🙂

edit

I just error_log and It show

Fatal error: Uncaught exception ‘MongoException’ with message ‘The MongoCursor object has not been correctly initialized by its constructor’ in /var/www/web/sendanswer.php:92 Stack trace: #0 /var/www/web/sendanswer.php(92): MongoCursor->rewind() #1 /var/www/web/sendanswer.php(343): sendanswer->updateperformancescore(4.4269230365753) #2 {main} Next exception ‘MongoException’ with message ‘The MongoCursor object has not been correctly initialized by its constructor’ in /var/www/web/sendanswer.php:92 Stack trace: #0 /var/www/web/sendanswer.php(92): MongoCursor->rewind() #1 /var/www/web/sendanswer.php(343): sendanswer->updateperformancescore(4.4269230365753) #2 {main} thrown in /var/www/web/sendanswer.php on line 92

what does it mean ??

edit

That means “Probably your Mongo object is not connected to a database server.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:23:40+00:00Added an answer on June 9, 2026 at 4:23 am

    What I see is an error in datatypes.

    In database user_ID is an integer,
    in your PHP query, it is a string.
    Try:

    $query2 = array("user_ID"=> 999);
    

    instead of :

    $query2 = array("user_ID"=>"999");
    

    PHP may be easy on datatypes, but I am not sure about Mongodb and its PHP driver.

    Also there is an incorrect parameter in your find statement. Remove the true as it should be an array of fields, not a bool

    This code works correctly :

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

    Output:

    [......]$ 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}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a separate function to get specific data from Facebook graph
I'm trying to get data from the database in order to create a list
Hello i made a php socket server to get data from plc, plc is
function check_timer(){ //get product ids var product_ids= document.getElementsByClassName(product_id); //create for loop for(var i=0; i<product_ids.length;
i want to create functions what get the folder size. I write small function:
I'm trying to create a procedure which extracts data from a MySQL server (using
(sorry for the english) I have a ASP .net webservice that get data from
I'm new to PHP and I am reading data from an excel spreadsheet and
In the code below, I am trying to create a function patient_count that is
I use this function below to process the array data I retrieve from my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.