$res = $db->query("SELECT COUNT(*) as cnt FROM table")->fetchAll(PDO::FETCH_ASSOC);
if ( $res[0]['cnt'] == 0 ) {
$db->query("
INSERT INTO table (col)
VALUES
('value1'),
('value2'),
('value3'),
....
");
}
Suppose 2 users requested this code same time,
So, for first user, count will return 0 and INSERT query will executed, but there is possible that while first insert executed, for second user, count return also 0 ? (and in this case second insert query will also executed, what I don't need).
If this is possible, how to prevent this? Using Transactions will help in such cases ?