I am new to php and I messed it up my code little bit. I would appreciate any help.
$sqltest="SELECT MaId,Amount FROM table1";
$stmttest = sqlsrv_query( $conn, $sqltest);
if( $stmttest === false) {
die( print_r( sqlsrv_errors(), true) );
}
while($test = sqlsrv_fetch_array( $stmttest, SQLSRV_FETCH_ASSOC)){ //fetch array
$combined[] = array($test['MaId']=>$test['Amount']); //pass the first column values as array keys
}
$loop=0;
foreach ($combined as $key => $value) {
foreach ($value as $msid => $msamount) {
while($msid!==25) {
$loop += $msamount;
}
}
}
I am trying to sum the all the amounts where the id is not equal to 25, but I'm getting stuck in an infinite loop. Can you explain what's going wrong?
The while
you have should be an if
if($msid!==25) {
$loop += $msamount;
}
Change:
while($msid!==25) {
to:
if ($msid!==25) {
And I have to agree with Mark Baker (commented above), if you can do this in the database, you should:
$sqltest = 'SELECT SUM(Amount) AS Amount FROM table1 WHERE MaId <> 25';
$stmttest = sqlsrv_query($conn, $sqltest);
if ($stmttest === false) {
die(print_r(sqlsrv_errors(), true));
}
$amount = 0;
if ($row = sqlsrv_fetch_array($stmttest, SQLSRV_FETCH_ASSOC)) {
$amount = $row['Amount'];
}