$dsn = 'mysql:dbname=lib;host=localhost';
$user = 'root';
$password = '';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Подключение не удалось: ' . $e->getMessage();
}
$sql = 'INSERT INTO uicheck (userid, book) VALUES (:userId, :book)';
$sth = $dbh->prepare($sql);
foreach ($dbh->query('SELECT userid FROM user) as $user) {
$userid = $user['userid'];
$data = file_get_contents('localhost/json' . $userid);
$json = json_decode($data);
$booksCount = $json->book;
if ($booksCount > 5) {
$sth->execute([
':userId' => $userid,
':book' => $booksCount
]);
}
}