2016-03-10 62 views
1

我在兩個不同的while循環中從數據庫中獲取數據,我想在循環之外在它們之間添加變量。例如:在兩個不同的while循環中添加數字

while($cash_fetch = mysql_fetch_array($cash_qur)) 
{ 
     $a = 500; //suppose I am fetching from database 
} 

while($card_fetch = mysql_fetch_array($card_qur)) 
{ 
    $b = 1000; //suppose I am fetching from database 
} 

$total = $a+$b; 

echo $total; 

我想要完全做這件事,但我得到不適當的結果。請幫忙。

回答

0

你可以試試這個 -

$a = $b = 0; // set to 0 by default 
while($cash_fetch = mysql_fetch_array($cash_qur)) 
{ 
    $a += 500; //Increment 
} 

while($card_fetch = mysql_fetch_array($card_qur)) 
{ 
    $b += 1000; //Increment 
} 
$total = $a + $b; 
echo $total; 
0

你可能想這樣做:

$total = 0; 
while($cash_fetch = mysql_fetch_array($cash_qur)) 
{ 
    ++$total; //Increment 
} 

while($card_fetch = mysql_fetch_array($card_qur)) 
{ 
    ++$total; //Increment 
} 

echo $total; // this will give you a count of total fetched records 
0

你可以用這個嘗試:

$a = 0; 
$b = 0; 
while($cash_fetch = mysql_fetch_array($cash_qur)) 
{ 
     $a = 500; //suppose I am fetching from database 
} 

while($card_fetch = mysql_fetch_array($card_qur)) 
{ 
    $b = 1000; //suppose I am fetching from database 
} 

$total = $a+$b; 

echo $total;