Jerome Axel Jerome Axel - 2 years ago 59
SQL Question

trying to get Salary for months March to December then add them up in mysql and php

$January = 'January';
$February = 'February';
$March = 'March';
$April = 'April';
$May = 'May';
$June = 'June';
$July = 'July';
$August = 'August';
$September = 'September';
$October = 'October';
$November = 'November';
$December = 'December';
$user = $_COOKIE['user'];
$Year = '2017';
$Year1 = '2016';

in the below code i am trying only to get the Salary field with their values were the months are from March to December but it returns null but in the database i can see there are values in the Salary field .

// total Salary
$result1 = mysqli_query($link, "SELECT Salary FROM $tb2_name WHERE Employee_Number='$user' AND Month='$March' AND Month='$April' AND Month='$May' AND Month='$June' AND Month='$July' AND Month='$August' AND Month='$September' AND Month='$October' AND Month='$November' AND Month='$December' AND Year='$Year1' ");

$row = mysqli_fetch_assoc($result1);

$test_sum = array_sum($row);

Answer Source

Change your query to the following

     $result1 = mysqli_query($link, "SELECT Salary FROM $tb2_name WHERE Employee_Number='".$user."' AND ( Month='".$March."' OR Month='".$April."' OR Month='".$May."' OR Month='".$June."' OR Month='".$July."' OR Month='".$August."' OR Month='".$September."' OR Month='".$October."' OR Month='".$November."' OR Month='".$December."') AND Year='".$Year1."' ");                                             

while( $row = mysqli_fetch_assoc($result1)){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download