L
L
leni_m2021-06-27 20:14:18
PHP
leni_m, 2021-06-27 20:14:18

Why does an error appear when performing multiple queries to the database?

There is a database with a large amount of data, and I want to sort through all of them using the function:

function get_data_db($count, $offset) {
    $servername = "localhost";
    $db = "fann";
    $u = "root";
    $p = "****";
    $mysqli = new mysqli($servername, $u, $p, $db);
    $_arr = [];
    $sql = "SELECT * FROM tbl ORDER BY r LIMIT $count OFFSET $offset";
    if ($res = $mysqli->query($sql)) {
        $res->data_seek(0);
        while ($row = $res->fetch_assoc()) {
            $arr['...'] = $row['...'];
            $_arr[] = $arr;
        }
    }
    $mysqli->close();
    return $_arr;
}

this function is looped. In order to go through all this data in turn, it needs to go through 6000 cycles, but already somewhere on the hundredth (always in different) cycle an error appears and the script crashes. It seems to be written $mysqli->close();and at the end of each cycle the connection is reset, from which the base should not be overloaded.
The error is like this:
60d8b1e357f48256639364.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2021-06-27
@leni_m

What is the need to close the connection to the database every time the function is executed?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question