Good day to you! I am looking for a way to implement the following: It is necessary to take data from a database on a remote server, mark the fields that were in the selection and insert them into the database on another server. My thoughts led me to the following: I added a column to the database from which I take the data IsUpdated = 0, in a cycle I take 300 IsUpdated in an array, UPDATE tables from which I took the data IsUpdated = 1, and INSERT into the final table. It is not possible to run the cycle until all the required rows have the number 1 in the IsUpdated field. The total of the code is 743 records in the database instead of 62 with a few thousand, and the second table is completely updated with IsUpdated instead of 0 becoming 1. I don’t understand what’s wrong . No timeout error messages.
do{ $mysqli = mysqli_connect('host', 'login', 'pass', 'db_1'); mysqli_query($mysqli, 'SET NAMES \'utf8\''); if ($mysqli->connect_error) { die('Ошибка подключения (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } $query = "SELECT row1, row2, row3 FROM `db_1`.`tabe_1` WHERE Result <='3' AND State = 'disabled' AND IsUpdated = 0 ORDER BY abc DESC LIMIT 300"; if(!$stmt = $mysqli->prepare($query)) { die('Ошибка выполнения запроса (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } $stmt->execute(); $stmt->bind_result($row1, $row2, $row3); $sql = array(); while ($stmt->fetch()) { $row1 = $mysqli->real_escape_string($row1); $row2 = $mysqli->real_escape_string($row2); $row3 = $mysqli->real_escape_string($row3); $sql[] = "('$row1', '$row2', '$row3')"; } $mysqliUpdate = "UPDATE `db_1`.`tabe_1` SET `IsUpdated` = 1 WHERE Result <='3' AND State = 'disabled' AND `IsUpdated` = 0 ORDER BY abc DESC LIMIT 300"; $mysqli->query($mysqliUpdate); $mysqli->close(); $mysqli1 = mysqli_connect('localhost', 'root', '', 'example'); mysqli_query($mysqli1, 'SET NAMES \'utf8\''); if ($mysqli1->connect_error) { die('Ошибка подключения (' . $mysqli1->connect_errno . ') ' . $mysqli1->connect_error); } if(count($sql) > 0) { $query1 = "INSERT INTO `db_2`.`table_2` VALUES " . implode(',', $sql) . " ON DUPLICATE KEY UPDATE row1 = VALUES(row1), row1 = VALUES(row2), row1 = VALUES(row3)"; $mysqli1->query($query1); $mysqli1->close(); } }while (count($sql) > 0);