Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';无法从mongoDB获取数据_Php_Mongodb - Fatal编程技术网

Php Can';无法从mongoDB获取数据

Php Can';无法从mongoDB获取数据,php,mongodb,Php,Mongodb,我创建了一个函数,通过PHP代码从mongoDB获取数据。但似乎效果不太好 让我们看看我的代码 function updateperformancescore($timepf) { $mon = new Mongo('mongodb://127.0.0.1:27017'); $dbs = $mon->selectDB('bitdindatabase'); $col = $dbs->selectCollection('bd_per

我创建了一个函数,通过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);
            }
为什么它不进入foreach?这段代码有什么问题,以及如何正确获取数据。 请帮助或指导我:)

编辑

我只是记录了一个错误,然后它就显示出来了

致命错误:未捕获异常“MongoException”,在/var/www/web/sendanswer.php:92堆栈跟踪:#0/var/www/web/sendanswer.php(92):MongoCorsor->rewind()#1/var/www/web/sendanswer.php(343):sendanswer->updateperformancescore(4.4269230365753)#2{main}下一个异常“MongoException”,在/var/www/web/sendanswer.php:92堆栈跟踪:#0/var/www/web/sendanswer.php(92):MongoCorsor->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");
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}
[…]$php testmongo.php

查询{“用户ID”:999,“时间”:999,“slug”:999} {“user_ID:999”,time:999,slug:999} {“user_ID:999”,time:339,slug:999} {“user_ID:999”,time:339,slug:5599}
PHP不会随意忽略您的foreach。如果它没有进入那里,那么它就没有什么可进入的,这意味着你的$data2不包含任何数据。我如何检查$data2中是否有数据???@crazyoxygen现在可以工作了。检查我编辑的答案。没有任何变化。我想我们可以使用“999”。@crazyoxygen:实际上,“999”(字符串)与999(整数)不同。。它们在中是不同的类型,除非您的驱动程序正在执行某些强制转换(默认情况下,PHP驱动程序不会这样做),否则它们将不匹配。在
mongo
shell中尝试,通过搜索999;-)将无法找到id“999”。哦,你说得对,但是我把php代码改成999,什么也没变。@crazyoxygen:那么你可能又有一个bug了,虽然我当时没有看到moment@Geert-Jan的另一个错误是,他在
find
命令中使用了不正确的参数。
<?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}