First, you're doing the job twice:$fila = mysqli_fetch_array($consulta); // obtienes la fila 1 como array numérico
// $fila es ["1", "2000"]
$arraySalarioViejo = array();
while ($fila = mysqli_fetch_array($consulta)) { // obtienes la fila 2
// $fila es ["2", "2000"]
foreach ($consulta as $value) { // extraes las 5 filas como array asociativo
// $values es, por ejemplo ['idEmpleado'=>1, 'Salario'=>2000]
$arraySalarioViejo[] = $value;
}
}
You're pulling out a row that's not going anywhere, a second row in the loop. while , then 5 rows in the foreach and while It was only once, because after foreach You emptied the stack.It would suffice to: $consulta = mysqli_query($dbViejo, $sql);
$arraySalarioViejo=[];
foreach ($consulta as $value) { // extraes las 5 filas como array asociativo
$arraySalarioViejo[] = $value;
}
(this in php 5.4+, before mysql_result was not iterable)To move the data, the healthiest thing is to create a statement associated with the old connection, and run it in each row of the new array.Using your procedure-focused code:$update_sentence="UPDATE empleado set Salario=? where idEmpleado=?";
if ($stmt = mysqli_prepare($dbViejo, $update_sentence)) {
foreach($arraySalarioNuevo as $nuevaData) {
/* asociar parámetros de la fila */
mysqli_stmt_bind_param(
$stmt, "ss",
$nuevaData['Salario'] ,
$nuevaData['idEmpleado']
);
/* ejecutar el statement en cada iteración */
if(!$update_result = mysqli_stmt_execute($stmt)) {
// Si hubo errores, mostrarlos
printf("Error: %s.\n", mysqli_stmt_error($sentencia));
}
}
/* cerrar el statement () */
mysqli_stmt_close($stmt);
}
This could be done even without a step array and without running a pick on the old BBDD, remaining:<?php
$dbViejo = mysqli_connect("localhost", "root", "", "cursoempresa");
/* verificar la conexión */
if (mysqli_connect_errno()) {
printf("Falló la conexión dbViejo: %s\n", mysqli_connect_error());
exit();
}
$dbNuevo = mysqli_connect("localhost", "root", "", "empresacurso");
if (mysqli_connect_errno()) {
printf("Falló la conexión dbNuevo: %s\n", mysqli_connect_error());
exit();
}
$select_sentence = "SELECT IdEmpleado,Salario FROM empleado";
$result = mysqli_query($dbNuevo, $select_sentence);
if (!$result) {
printf("Error: %s.\n", mysqli_error($dbNuevo));
exit();
}
$update_sentence = "UPDATE empleado set Salario=? where idEmpleado=?";
if ($stmt = mysqli_prepare($dbViejo, $update_sentence)) {
foreach ($result as $nuevaData) {
var_dump($nuevaData);
// asociar parámetros de la fila
mysqli_stmt_bind_param(
$stmt, "sd",
$nuevaData['Salario'] ,
$nuevaData['idEmpleado']
);
// ejecutar el statement en cada iteración
if (!$update_result = mysqli_stmt_execute($stmt)) {
printf("Error: %s.\n", mysqli_stmt_error($sentencia));
exit();
}
}
// cerrar el statement ()
mysqli_stmt_close($stmt);
}
mysqli_free_result($result);
mysqli_close($dbNuevo);
mysqli_close($dbViejo);
Edit I added bug handling to help you debug.Edit 2 I corrected the last example. He had fields with other names