简体   繁体   中英

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). But somehow insert returns an error after first successful one:

"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).

$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. You should use $collection->insert(); instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM