2013-03-05 78 views
1

我想從數據庫中獲取數據,直到滿足特定條件。如果滿足這個條件,我想在那個點之後停止提取。停止從某個點獲取數據庫中的數據

while($row=mysql_fetch_array($result1)){ 
$a1=$row['Count'] 
if($a1<100){ 
$w1=$a1 
//Now I want to stop fetching data after this point and take the variable "$w1" out 
} 
+0

只需使用exit;滿足條件後 – 2013-03-05 07:05:23

+1

使用'break;'退出循環。 – Rikesh 2013-03-05 07:05:53

+1

@PavanK'exit'會停止一切,'break'更好 – 2013-03-05 07:06:37

回答

3

您可以使用break

while ($row = mysql_fetch_array($result1)) { 
    $a1 = $row['Count']; 

    if ($a1 < 100) { 
     $w1 = $a1; 
     break; 
    } 
} 

或者return出來的函數。

3

最好的選擇是給limitswhere條件查詢

,以便查詢執行會快(你需要獲取的數據較少)。

不是首先獲取整個數據並對其進行過濾,先過濾數據並取回數據。

你的情況:如果你想取前100條記錄:

$sql = "SELECT * FROM table LIMIT 0,100"; 

如果你有那麼任何條件,

$sql = "SELECT * FROM table WHERE field = '".$var."' LIMIT 0,100"; 
0

使用break;或更好的你可以使用限制&在你的查詢條件

while($row=mysql_fetch_array($result1)) 
{ 
    $a1=$row['Count']; 
    if($a1<100) 
    { 
    $w1=$a1; break;   
    } 
}