I
I
Ilya Kochkin2018-02-08 23:04:29
PHP
Ilya Kochkin, 2018-02-08 23:04:29

How to execute sql query 10000 times?

There is an array with data, approximately 10,000 elements (not the limit) - I get VK users with fields (user_id, name, photo, total_score) through vk api. It is required to add them to the database, and if there is already a user with user_id in the database, we update the data; if the user does not exist, then we insert it into the database

$connection = mysqli_connect($servername, $username, $password, $bdname);
    foreach ($user_arr as user) {//over 10k
    $sql = "SELECT * FROM top_users WHERE usd='".$user[id]."' AND date='".$date."'";
    $result = mysqli_query($connection, $sql);
    $data = mysqli_fetch_all($result, MYSQLI_ASSOC);
    if($data[0]){
        $sql="UPDATE top_users SET total_score='".$user[total_score]."' WHERE usd='".$user[id]."' AND date='".$date."'";
        mysqli_query($connection, $sql);
    }else{
        $sql="INSERT INTO top_users (ID,usd,name,img,total_score,date) VALUES  (NULL,'".$user[id]."','".$user[name]."','".$user[photo]."','".$user[total_score]."','".$date."')";
        mysqli_query($connection, $sql);
         }

    }

so I get this error Warning: Error while sending QUERY packet. PID=11076 in C:\OSPanel\domains\localhost\Config.php on line 38
the actual line is $result = mysqli_query($connection, $sql);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
poznavaka, 2019-12-14
@poznavaka

10 thousand additions to perform once is not a problem, BUT, in mysql they came up with the INSERT ... ON DUPLICATE KEY UPDATE ... construction for such purposes. Make user_id from VK as a primary key (by the way, in VK user_id is bigint as far as I know) and mysql will thank you. This way, there will be no extra select, the clustered index will work very efficiently, and your code will become cleaner.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question