I'm bringing a query from one bank and I'm going to insert it into another one, this is running the question and it's 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();
?>