A
A
Ambrel2018-07-03 11:06:27
PHP
Ambrel, 2018-07-03 11:06:27

Reduce the number of queries in the database?

Basically, I have this code:

function insertGameMysql() {
  global $arrGame;
  global $link;
  for ($i = 1; $i < count($arrGame)+1; $i++) { 
    $query = "('".$i."','".trim($arrGame[$i][0])."','".$arrGame[$i][1]."','".$arrGame[$i][2]."','".$arrGame[$i][3]."','".$arrGame[$i][4]."','".$arrGame[$i][5]."','".$arrGame[$i][6]."','".$arrGame[$i][7]."')";
    $stmt = $link->query('INSERT INTO `games` VALUES '.$query);
  }
  exit();
}

And this code with the help of js runs every 30 seconds. Naturally, the data in $arrGame changes every time - commonly. number of data 120! That is, 120 requests are sent to the database every 30 seconds. How to reduce it? I read that it's better not to put a query in the database in a cycle, but how can I do without a cycle?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2018-07-03
@alexey-m-ukolov

INSERT INTO games (one, two, three) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);
https://dev.mysql.com/doc/refman/5.7/en/insert.html

R
RidgeA, 2018-07-03
@RidgeA

You can get by with one insert
And in the above code, the vulnerability is SQL Injection - read about the use of prepared queries.

M
mShpakov, 2018-07-03
@mShpakov

1) multiple insert INSERT INTO a VALUES (1,23),(2,34),(4,33);
2) use orm (e.g. eloquent)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question