PHP PDO,在foreach中绑定params而没有& - 奇怪的结果

时间:2016-05-10 13:20:40

标签: php pdo foreach

我发现了一个PDO的奇怪行为。 当我使用此代码时

$url_array = ['111', '222'];
foreach ($url_array as $key => $url){
    $values[] = '(:url'.$key.', :referer'.$key.')';
}
$query = implode(',', $values);

$pdo = $this->createPDO();
$sth = $pdo->prepare("INSERT INTO `tbl_list` (`num`) VALUES " . $query);

foreach ($nums as $key => $num){     <==================
     $sth->bindParam(':url'.$key, $num);
}
$sth->execute();

我在桌子上得到222,222。但是当我添加&amp;到了2-d foreach-我的表中有111,222

$url_array = ['111', '222'];
foreach ($url_array as $key => $url){
    $values[] = '(:url'.$key.', :referer'.$key.')';
}
$query = implode(',', $values);

$pdo = $this->createPDO();
$sth = $pdo->prepare("INSERT INTO `tbl_list` (`num`) VALUES " . $query);

foreach ($nums as $key => &$num){     <==================
     $sth->bindParam(':url'.$key, $num);
}
$sth->execute();

这是什么原因?

0 个答案:

没有答案