使用MongoDB和PHP在集合中没有数据时获取错误

时间:2016-08-22 07:30:38

标签: php mongodb

即使我的MongoDB集合为空,我的数组长度为1。我正在使用PHP和MongoDB。

代码:

$listdata=$db->kf_poll_master->find();

 echo (count($listdata));exit;

这里我的收藏:kf_poll_master没有数据,数组长度在echo中显示为1,它应该为0。

1 个答案:

答案 0 :(得分:3)

var parseString = require('xml2js').parseString; var xml = '<img title=\'A San Bernardino County Fire Department firefighter watches a helitanker make a water drop on a wildfire, seen from Cajon Boulevard in Devore, Calif., Thursday, Aug. 18, 2016. (David Pardo/The Daily Press via AP)\' height=\'259\' alt=\'APTOPIX California Wildfires\' width=\'460\' src=\'http://i.cbc.ca/1.3730399.1471835992!/cpImage/httpImage/image.jpg_gen/derivatives/16x9_460/aptopix-california-wildfires.jpg\' />'; parseString(xml, function (err, result) { console.log(JSON.stringify(result, null, 4)); console.log(result["img"]["$"]["src"]); }); 不返回PHP数组,而是返回类MongoCursor的对象。您不能使用本机PHP计数()。

你应该这样做:

MongoCollection::find()

或者,您可以使用Iterator将MongoCursor(iterator_to_array)转换为数组。