我有一个从csv文件创建的数组,相关的代码是:
foreach($data1 as $key => $val){
$data[] = ['country' => $val['Country Name'], 'iso_code' => $val['ISO2'],'phone_code' => $val['Phone Code'],'continent' => $val['Continent'],'capital' => $val['Capital'],'time_zone' => $val['Time Zone in Capital'],'currency' => $val['Currency']]; // so on and so forth.
}
//print_r($data);
我在以下key->值对中得到数组:
Array ( [0] => Array ( [country] => Afghanistan [iso_code] => AF [phone_code] => 93 [continent] => Asia [capital] => Kabul [time_zone] => Asia/Kabul [currency] => Afghani ) [1] => Array ( [country] => Albania [iso_code] => AL [phone_code] => 355 [continent] => Europe [capital] => Tirana [time_zone] => Europe/Tirane [currency] => Lek ) [2] => Array ( [country] => Algeria [iso_code] => DZ [phone_code] => 213 [continent] => Africa [capital] => Algiers [time_zone] => Africa/Algiers [currency] => Dinar ) [3] =>
.......等等......
我正在尝试使用以下代码在我的数据库表中插入值:
$conn->beginTransaction();
$sql = "INSERT INTO countries (country,iso_code,phone_code,continent,capital,time_zone,currency) VALUES (?,?,?,?,?,?,?)";
try {
$stmt = $conn->prepare($sql);
foreach ($data as $v){
$stmt->execute(array_values($v));
}//end foreach
$conn->commit();
} catch (PDOException $e) {
}//end catch
$stmt->closeCursor();
$conn = NULL;
数据未插入,也没有出现任何错误。我用foreach循环做错了吗?无法确定相同的原因。我是一个php新手。