php while while if if for first row else for second row?

时间:2015-07-25 06:15:02

标签: php mysql if-statement while-loop

我有来自mysql的while循环,我尝试为第一个结果做一些事情,如果否则为第二个结果做其他事情等....

我的代码

$i = 1;
while($row = mysql_fetch_array($get_row)) {
$rowid = $row['rowid'];
$number = $row['number'];
$result = $number - 1;
if ($result != '0') {
// for only first result != 0 do this
require('result.php');
$i++;
// for all other results != 0 too do this
$edit_row = "UPDATE rows SET status = 'Ok' WHERE rowid = '$rowid'";
mysql_query($edit_row);
} elseif ($result == '0') {
$edit_row = "UPDATE rows SET status = 'Not Ok' WHERE rowid = '$rowid'";
mysql_query($edit_row);
} 
}

1 个答案:

答案 0 :(得分:1)

只需通过增加$i即可。当$i == 1做其他事情时,做第一行。例如:

$i = 1;
while($row = mysql_fetch_array($get_row)) {
    $rowid = $row['rowid'];
    if ($i == 1) { //Do for first row when $i == 0
        $i++; //Increase the value of $i
        require('result.php');
        $edit_row = "UPDATE rows SET status = 'Ok' WHERE rowid = '$rowid'";
        mysql_query($edit_row);
    } else { //Do other when $i > 1
        $edit_row = "UPDATE rows SET status = 'Not Ok' WHERE rowid = '$rowid'";
        mysql_query($edit_row);
    }
}

还建议您使用mysqliPDO代替mysql