0
I’m working on an archive import .txt
where the elements are separated by |
. So far so good, the file is interpreted, the values are stored in an array and then I need to enter the values in the database. For this, I created small functions with the aim of separating the responsibilities.
That’s where the problem comes in, let’s put it this way.
function inserir_notas_entrada($fornecedores, $notas, $arquivo) {
require("utils/conexao.php");
require_once("utils/util.php");
$query = "";
foreach ($notas as $nota => $a) {
$part = function () use ($a, $conn, $fornecedores) {
foreach ($fornecedores as $fornecedor => $f) {
if ($f[1] == $a[3]) {
$cnpj = ($f[4] != "") ? formatar_cnpf_cnpj($f[4]) : formatar_cnpf_cnpj($f[5]);
$busca_fornecedor = mysqli_query($conn, "SELECT CODIGO_FORNECEDOR FROM fornecedores WHERE CNPJ_CPF = '$cnpj'");
$dados = mysqli_fetch_assoc($busca_fornecedor);
}
}
return $dados['CODIGO_FORNECEDOR'];
};
$serie = $a[6];
$num_doc = $a[7];
$chave = $a[8];
$dt_doc = date("Y-m-d", strtotime(formatar_data_sped($a[9])));
$dt_e = date("Y-m-d", strtotime(formatar_data_sped($a[10])));
$val_doc = floatval(str_replace(",", ".", $a[11]));
$val_desc = floatval(str_replace(",", ".", $a[13]));
$val_itens = floatval(str_replace(",", ".", $a[15]));
$val_outras = floatval(str_replace(",", ".", $a[21]));
$verifica_chave_nota = mysqli_query($conn, "SELECT CHAVE_NFE FROM entradas WHERE CHAVE_NFE = '$chave'");
if (mysqli_num_rows($verifica_chave_nota) <= 0) {
$cod_f = $part();
echo $cod_f."</br>";
$query .= "INSERT INTO entradas (DATA_ENTRADA, DATA_NOTA, CHAVE_NFE, NRO_NOTA, SERIE_NOTA, COD_FORNECEDOR, TOTAL_PRODUTOS, TOTAL_OUTRAS, TOTAL_DESCONTO, TOTAL_NOTA, ARQUIVO)
VALUES ('$dt_e', '$dt_doc', '$chave', '$num_doc', '$serie', '$cod_f', '$val_itens', '$val_outras', '$val_desc', '$val_doc', '$arquivo');\n";
}
}
}
When I perform the above function, her return is this:
However, when I run the code through debug, the result is different (and also correct):
I have found two ways to solve this, but I don’t think they are ideal. The first would be to run the query directly in the foreach loop. The second option is to include a small usleep()
, but would compromise speed depending on the amount of information.
There’d be some way to fix it?
NOTE: The tests are being carried out locally.
Already checked the production server timeout ?
– Marcos Brinner
Would be the directive
innodb_lock_wait_timeout
found mo my.ini? If it is set at 50...– Yuri Freitas