Yoran Yoran - 2 months ago 75
MySQL Question

Notice: Undefined offset: 1 in line 23

For a school project I needed to change my MySQL database to a MongoDB.

This is my old code that is working fine:

$movies = '';
$count = 0;

while($row_movie = mysqli_fetch_assoc($result)){
$count++;

$movies[$count] .= "{ value: '".$row_movie['naam']."', data:'".$row_movie['idProduct']."' }";

}


Now I made a foreach loop to handle the MongoDB collection:

$collection = $colProduct;
$products = $collection->find();

$result = $collection->find();

$movies = '';
$count = 0;
foreach($result as $row_movie){
$count++;
$movies[$count] .= $row_movie['naam'];
print_r($movies);
}


But now I get the error: Notice: Undefined offset: 1 in D:\wamp64\www\examen.yvde.nl\currency-autocomplete.php on line 23

(this line: $movies[$count] .= $row_movie['naam'];)

I hope anyone can help me out!
Thanks :)

Answer Source

Method A: export from MySQL in a CSV format and then use the mongoimport tool. However, this does not always work well in terms of handling dates of binary data.

Method B: script the transfer in your language of choice. Basically you write a program that reads everything from MySQL one element at a time and then inserts it into MongoDB.

Method B is better than A, but it is still not adequate.

$movies = array();
$count = 0;

while($row_movie = mysqli_fetch_assoc($result)){
$count++;

$movies[$count] .= "{ value: '".$row_movie['naam']."', 
data:'".$row_movie['idProduct']."' }";
}

The MongoDB collection:

$collection = $colProduct;
$products = $collection->find();
$result = $collection->find();

$movies = array();
$count = 0;
foreach($result as $row_movie){
$count++;
$movies[$count] = $row_movie['naam'];
print_r($movies);
}