简体   繁体   English

为什么我的MongoDB函数出现致命错误?

[英]Why am I getting a Fatal error with this MongoDB function?

I've got a simple php script for getting a csv file into array and inserting each row into MongoDB (CD Collection). 我有一个简单的PHP脚本,用于将csv文件放入数组并将每一行插入MongoDB(CD集合)中。 But somehow insert returns an error after first successful one: 但是以某种方式,insert在第一个成功执行成功后会返回错误:

"Fatal error: in C:\xampp\htdocs\mongo\lesson1\index.php on line 17"

Here's the code. 这是代码。 What could possibly cause such an error? 可能导致这种错误的原因是什么? DB receives only one (first). DB仅接收一个(第一个)。

$filename = 'd:/cd_col.csv'; // Each line: Title;No. of CD with movie
$csvfile = fopen($filename,'rb'); 
while(!feof($csvfile)) {
    $csvarray[] = fgetcsv($csvfile);
}
$m = new MongoClient();
$db = $m->mymovies;
$collection = $db->movies;
$id=0;
foreach($csvarray as $key=>$value)
{
    $movie = explode(';', $value[0]);
    $fmovie = array('_id'=>++$id, 'title'=>$movie[0], 'cdno'=>$movie[1]);
    if($collection->save($fmovie)===true) { // this is line 17
        echo 'Successful insert: '.$key;
    }
}

SOLVED: 解决了:

Cannot use save in php, that's it. 无法在PHP中使用save,仅此而已。 You should use $collection->insert(); 您应该使用$ collection-> insert(); instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现此错误? 致命错误:找不到类“ TestCase” - Why am I getting this error? Fatal error: Class 'TestCase' not found 为什么我在Laravel中遇到此错误:PHP Catchable致命错误? - Why am I getting this error with Laravel: PHP Catchable Fatal Error? 为什么两次调用相同的函数会导致php致命错误? - why am i getting a php fatal error for calling the same function twice? 为什么会出现“致命错误:在非对象上调用成员函数fetch_assoc()”? - Why am I getting a “Fatal error: Call to a member function fetch_assoc() on a non-object”? 为什么会出现此Spreadsheet_Excel_Writer致命错误:使用writeFormula()的最大函数嵌套级别? - Why am I getting this Spreadsheet_Excel_Writer Fatal error: Maximum function nesting level with writeFormula()? 为什么我在 WordPress 上遇到致命的内存错误? - Why am I getting a fatal memory error on WordPress? 为什么会出现致命错误:不在对象上下文中时使用$ this? - Why am I getting the Fatal error: Using $this when not in object context? 为什么,我用注释$ start var得到致命错误? - Why, I am getting a Fatal Error with commented $start var? 为什么我的代码出现PHP致命错误? - Why am I getting a PHP Fatal error with my code? 为什么在此php代码中出现致命错误: - Why am I getting Fatal error: in this php code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM