我有一个php数组的问题,我以前没有见过。我从数据库中获取数据并构建一个数组,以便将所有数据传输到另一个数据库。我的问题是,我有大约10万行要放入数组并传输它,但最终我的数组只包含12行。没有错误报告,一切正常,但有12条记录,而不是100k。
请看一下我的代码,给我指出问题所在。我已经花了好几天的时间,我想不出来。数组有什么限制吗?
我还增加了dev服务器上的最大内存和最大执行时间,这样就不会给我带来任何问题,但没有任何帮助。
<?php
include("../includes/session.php");
//Here we set connection to Gimli MySQL server
//Connect to DPB DB
$servername = "X";
$username = "X";
$database = "X";
$password = "X";
// Create a new connection to the MySQL
$conn_gimli = new mysqli($servername, $username, $password);
// Check connection
if ($conn_gimli->connect_error) {
die("Connection failed: " . $conn_gimli->connect_error);
} else {
echo "Connected successfully<br>";
}
//Get pricelist headers from iManage pricelist
$pricelist_header = array();
if($stmt = $conn_gimli -> prepare("SELECT id, YEAR(vrijeme), naziv FROM servis.kalkulacija_import_cjenik")) {
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($pricelist_id, $pricelist_year, $pricelist_name);
while($stmt ->fetch()) {
$pricelist_header[] = array('header_year' => $pricelist_year,
'header_name' => $pricelist_name,
'header_id' => $pricelist_id);
}
} else {
$error = $conn -> errno . ' ' . $conn -> error;
echo $error;
die;
}
//Get pricelist items from iManage pricelist
foreach($pricelist_header as $key => $value) {
$pricelist_items = array();
if($stmt = $conn_gimli -> prepare("SELECT id_cjenika, godina, kataloski_broj, naziv, cijena_EUR, valuta FROM servis.kalkulacija_import_cjenik_stavke WHERE id_cjenika = ?")) {
$stmt -> bind_param("i", $value['header_id']);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($item_id, $item_year, $item_article_no, $item_name, $item_price, $item_currency);
while($stmt -> fetch()) {
$pricelist_items[] = array('item_id' => $item_id,
'item_year' => $item_year,
'item_article_no' => $item_article_no,
'item_name' => $item_name,
'item_price' => decimal_convert($item_price),
'item_currency' => $item_currency);
}
$stmt -> close();
} else {
$error = $conn -> errno . ' ' . $conn -> error;
echo $error;
die;
}
}
//View arrays
//var_dump($pricelist_header);
//var_dump($pricelist_items);
//Insert headers and items in DB
foreach($pricelist_header as $key => $value) {
//Insert header and get inserted id
if($stmt = $conn -> prepare("INSERT INTO dpb.calculation_pricelist (name, for_year) VALUES (?, ?)")) {
$stmt -> bind_param("si", $value['header_name'], $value['header_year']);
$stmt -> execute();
$stmt -> store_result();
$last_id = $stmt -> insert_id;
$stmt -> close();
} else {
$error = $conn -> errno . ' ' . $conn -> error;
echo $error;
die;
}
if(!empty($last_id)) {
foreach($pricelist_items as $ikey => $ivalue) {
//Insert items for header
if($stmt = $conn -> prepare("INSERT INTO dpb.calculation_pricelist_articles (pricelist_id, article_no, description, normal_price, currency) VALUES (?, ?, ?, ?, ?)")) {
$stmt -> bind_param("issis", $last_id, $ivalue['item_article_no'], $ivalue['item_name'], $ivalue['item_price'], $ivalue['item_currency']);
$stmt -> execute();
$stmt -> close();
} else {
$error = $conn -> errno . ' ' . $conn -> error;
echo $error;
die;
}
}
}
}
?>
1条答案
按热度按时间0s0u357o1#
我注意到了一些错误,虽然我已经用一种我认为对您有用的格式重写了代码,但我认为错误同样可能是您忘记关闭连接,所以它不会遍历每个值,只遍历一个值。因此,也许在12行中,实际上得到了6个键=>值对(6*2)。尽管这在我看来有点疯狂的猜测,这取决于你如何判断你的代码不起作用。
以下代码应该适合您: