I want to update thousands of rows when the user click on a submit button, my function use another functions that calculates the field that I want to update. My problem is when I try to update a few rows like 5 or 10 it works fine but when I have like 20 rows or more, it takes a long time to execute the code or sometimes, it displays a lot of errors that I don't have. So can anyone help me please!
My code:
//----database connection
/* DATABASE CONFIGURATION */
define('DB_SERVER', 'localhost');
define('DB_PORT', '8090');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'db_qvt');
function getDB(){
$dbhost=DB_SERVER;
$dbport=DB_PORT;
$dbuser=DB_USERNAME;
$dbpass=DB_PASSWORD;
$dbname=DB_DATABASE;
// Check connection
$dbConnection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
mysqli_set_charset($dbConnection, "utf8");
return $dbConnection;
}
//------------ update user final score--------------//
function update_user_final_score($login, $pass_user, $final_score){
$sql_update="update users_answers set score_final = '$final_score' where login = '$login' and pass_user = '$pass_user'";
$res = mysqli_query(getDB(), $sql_update);
return $res;
}
//------------ calculate all users final scores--------------//
function update_all_users_final_score($login){
$users_client = get_users_answers($login);
for($i=0;$i<sizeof($users_client);$i++){
update_user_final_score($users_client[$i]['login'], $users_client[$i]['pass_user'], calculate_user_final_score($users_client[$i]['login'], $users_client[$i]['pass_user']));
}
}
//-- call the function
update_all_users_final_score($login);