我想从数据库中获取数据,直到满足特定条件。如果满足这个条件,我想在那个点之后停止提取。停止从某个点获取数据库中的数据
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
}
我想从数据库中获取数据,直到满足特定条件。如果满足这个条件,我想在那个点之后停止提取。停止从某个点获取数据库中的数据
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
}
您可以使用break
:
while ($row = mysql_fetch_array($result1)) {
$a1 = $row['Count'];
if ($a1 < 100) {
$w1 = $a1;
break;
}
}
或者return
出来的函数。
最好的选择是给limits
和where
条件查询
,以便查询执行会快(你需要获取的数据较少)。
不是首先获取整个数据并对其进行过滤,先过滤数据并取回数据。
你的情况:如果你想取前100条记录:
$sql = "SELECT * FROM table LIMIT 0,100";
如果你有那么任何条件,
$sql = "SELECT * FROM table WHERE field = '".$var."' LIMIT 0,100";
使用break;或更好的你可以使用限制&在你的查询条件
while($row=mysql_fetch_array($result1))
{
$a1=$row['Count'];
if($a1<100)
{
$w1=$a1; break;
}
}
只需使用exit;满足条件后 – 2013-03-05 07:05:23
使用'break;'退出循环。 – Rikesh 2013-03-05 07:05:53
@PavanK'exit'会停止一切,'break'更好 – 2013-03-05 07:06:37