How to create an sql query using the column names as an array?

My code
average_col function(){

$link = mysql_connect('localhost', 'root', ");
$fields = mysql_list_fields("registrator", "grade_in_the_group", $link);
$columns = mysql_num_fields($fields);
$arr_rating = array();
for ($i = 3; $i < $columns; $i++) {
$last_name_colum = mysql_field_name($fields, $i);
$arr_rating[] = $last_name_colum."
";

}
print_r($arr_rating);
echo "
".count($arr_rating);

$exp = substr($last_name_colum, -1);
$rating_add = "rating_".($exp + 1);
if($i >= 10){
$exp = substr($last_name_colum, -2);
$rating_add = "rating_".($exp + 1);
}
}
average_col();

Should get a request of this type
SET `average`=(`rating_1`+ `rating_2` + `rating_3` + `rating_4`+ `rating_5`+ `rating_6`)/6
The data in this array all column names in database
Can't understand how can I write the code so that the array of output names in the query and between them stood the +sign
July 2nd 19 at 17:26
1 answer
July 2nd 19 at 17:28
Solution
assert('!empty($fieldNames) and is_array($fieldNames)');
$query = 'set field_name = (' . join(' + ', $fieldNames) . ')/' . count($fieldNames);


Probable error of designing of the database schema. With the use of long-deleted functions, mysql_* the probability of error, not a meaningful choice increases dramatically.

Find more questions by tags MySQLPHP