注意:未定义的偏移量:第23行中的1

时间:2017-08-20 12:15:13

标签: php mysql mongodb foreach database

对于学校项目,我需要将MySQL数据库更改为MongoDB。

这是我的旧代码,工作正常:

$movies = '';
$count = 0;

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

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

}

现在我创建了一个foreach循环来处理MongoDB集合:

$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);
}

但现在我收到错误:注意:第23行的D:\ wamp64 \ www \ examen.yvde.nl \ currency-autocomplete.php中未定义的偏移量为1

(这一行:$ movies [$ count]。= $ row_movie ['naam'];)

我希望有人能帮助我! 谢谢:))

1 个答案:

答案 0 :(得分:0)

您要将字符串附加到数组。将您的代码更改为

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

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

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

}

收集

$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);
}