数组不包含数据库中的所有值

时间:2018-11-07 12:32:51

标签: php mysql arrays

我对php数组有疑问,之前从未见过。我正在从数据库中获取数据并构建一个数组,以便可以将所有数据传输到另一个数据库中。我的问题是我大约有10万行要放入数组并传输它,但是最后我的数组仅包含12行。 没有错误报告,一切正常,但有12条记录而不是100K。

请看一下我的代码,并指出问题所在。我已经花了几天时间,无法弄清楚。数组有什么限制吗?

我还增加了我的开发服务器上的最大内存和最大执行时间,因此这对我没有任何问题,但这没有帮助。

    <?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 个答案:

答案 0 :(得分:0)

我注意到了一些错误,尽管我以我认为对您有用的格式重写了代码,但我认为错误同样可能是您忘记了关闭连接,所以它不会遍历每个对象。值,只有一个。因此,也许到了12行,您实际上得到了6个键=>值对(6 * 2)。 尽管这在我看来有些疯狂,但这取决于您确定代码无法正常工作的方式。

以下代码应该更适合您:

//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()) {
    // We push the values to our array, rather than re-declaring it.
    array_push($pricelist_header, array('header_year' => $pricelist_year,
                                        'header_name' => $pricelist_name,
                                        'header_id' => $pricelist_id);         
        }
    } 

    else {
    $error = $conn -> errno . ' ' . $conn -> error;
    die($error);
}
$stmt->close(); // This was where you forgot to close the connection.

// ****** Removed a duplicate block of code, here. *****

// Declare array outside of loop.
$pricelist_items = array();

//Get pricelist items from iManage pricelist
foreach($pricelist_header as $key => $value) {

    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()) {
        // We push the values to our array, rather than re-declaring it.

        array_push($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;
        die($error);
    }
}

// Declare array outside of your loop
$pricelist_items = array();

//Get pricelist items from iManage pricelist
foreach($pricelist_header as $key => $value) {        
    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()) {
            // We push the values to our array, rather than re-declaring it.
            array_push($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;
        die($error);
    }
}

//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;
        die($error);
    }

    if(!empty($last_id)) {
       // There was a spelling error here, instead of $key it said $ikey
        foreach($pricelist_items as $key => $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;
                die($error);
            }
        }
    }
}
?>