Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question