-1
I am bringing a query from one bank and I will insert in another,this running the question and that is only inserting the first line and not all the results,see my script below:
<?php
$host="192.168.0.249";
$port=3306;
$socket="";
$user="root";
$password="";
$dbname="db1";
$conEmp = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
$query = "select store_key as Loja, d.name as Departamento, sum(quantity) as Qtde, sum(amount) as Venda, sum(cost) as Custo, sum(margin) as MargemBruta, pos_number as Caixa from accum_item as i left join department as d on i.department_key = d.department_key where store_key in (1,2,4,5) and pos_number >0 and fiscal_date between cast('2016-09-20' as DATE) and cast('2016-09-20' as DATE) group by store_key, i.department_key,pos_number order by i.department_key";
if ($stmt = $conEmp->prepare($query)) {
$stmt->execute();
$stmt->bind_result($Loja, $Departamento, $Qtde, $Venda, $Custo, $MargemBruta, $Caixa);
while ($stmt->fetch()) {
$v_loja = $Loja;
$v_dep = $Departamento;
$v_qtde = $Qtde;
$v_venda = $Venda;
$v_custo = $Custo;
$v_margem = $MargemBruta;
$v_caixa = $Caixa;
}
$stmt->close();
}
$conEmp->close();
$host="192.168.0.210";
$port=3306;
$socket="";
$user="root";
$password="";
$dbname="db2";
$conEmp2 = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
mysqli_query($conEmp2,"INSERT INTO importacao (imp_id,
imp_loja,
imp_dep,
imp_qtde,
imp_venda,
imp_custo,
imp_margem,
imp_cx)
VALUES ('',
'$v_loja',
'$v_dep',
'$v_qtde',
'$v_venda',
'$v_custo',
'$v_margem',
'$v_caixa')");
$conEmp2 ->close();
?>
And why do you think this code should insert more than one line, if you are running a single Insert with a set of VALUES only?
– Bacco
Why declare connection attributes twice, if you had already declared them at the beginning of the code?
– user28595
@diegofm are 2 different servers
– Bacco
@Bacco only two changing attributes.
– user28595
@diegofm really in theory it could change only these two, the good thing of separating is if it is something subject to reuse, not to forget to change anything. In fact, in this case I think it’s best to go right inside the function, with no extra variable. Even this port and socket so much left, since they are with default value can be omitted.
– Bacco
@Otaciobarbosa the way you were doing you should put the Insert inside the while. It’s not the right one, the more it would work.
– Willian Coqueiro