Proper way of using array elements in the query

Viewed 50

Currently I have an array which elements are used in the mysql query. So far this gives the desired results but I believe that this probably it isn't the proper and optimal way of using it.

$myArray = array('arr1', 'arr2', 'arr3', 'arr4', 'arr5', 'arr5');
foreach ( $myArray as $my_array ) {
    $element[] = $my_array;
    
}
$query = "SELECT 
            $element[0] * 100 /($element[0] + $element[1] + $element[2] + $element[3] + $element[4] + $element[5]) AS val_1, 
            $element[1] * 100 /($element[0] + $element[1] + $element[2] + $element[3] + $element[4] + $element[5]) AS val_2
            FROM data_point_table 
            WHERE YEAR = $year 
            AND $element[0] IS NOT NULL 
            AND $element[1] IS NOT NULL 
            AND $element[2] IS NOT NULL 
            AND $element[3] IS NOT NULL 
            AND $element[4] IS NOT NULL 
            AND $element[5] IS NOT NULL";

The question:

What will be the proper way to do this because in the future there can have more array elements and at some point probably will be hard to maintain/update it.

0 Answers
Related