0

我有一组页面,它们有两个边界(来自用户输入),这会产生一个值,然后将其放置在 mysql 表中。然后将该值用作边界,该过程自行重复,创建一个新值并将其添加到 sql 表中。

现在,这个过程有一个精确的顺序,我尝试使用 if/else if 语句对它们进行排序:

$result = mysql_query("SELECT * FROM partitions 
WHERE subject_id = $subject_id AND stimulus = $stimulus", $DB);

if(!result) {
die("Database query failed: " . mysql_error());}

    while ($row = mysql_fetch_array($result)){

    if($row[6] == NULL){

    $query1  = "UPDATE partitions SET t_1_2 = $t_half WHERE subject_id = $subject_id AND stimulus = $stimulus";
    mysql_query($query1) or trigger_error(mysql_error()." in ".$query1);

    $target_page = "pan00";
    $next_t_a = $row[2];   // from user input
    $next_t_b = $t_half;   // can i replace $t_half by $row[6]?

}


elseif($row[4] == NULL)
    {

    $query1  = "UPDATE partitions SET t_1_4 = $t_half WHERE subject_id = $subject_id AND stimulus = $stimulus";
    mysql_query($query1) or trigger_error(mysql_error()." in ".$query1);

    $target_page = "pan00";
    $next_t_a = $row[2];
    $next_t_b = $row[4]; }


elseif($row[3] == NULL)
    {

    $query1  = "UPDATE partitions SET t_1_8 = $t_half WHERE subject_id = $subject_id AND stimulus = $stimulus";
    mysql_query($query1) or trigger_error(mysql_error()." in ".$query1);

    $target_page = "CE";
    $next_t_a = $row[4];
    $next_t_b = $row[6]; }

我认为使用 else if 语句会检查条件是否为真,如果是,则只需执行括号中的代码并跳过循环,但这里 $row[6] 和 $row[4] 都会同时更新。这与我为获取行值而设置的 while 循环有关吗?

我尝试使用 goto 和 break 但无济于事。

问题出在 while 循环中(感谢那些指出它的人),这让我想到了下一个问题:没有这个 while 循环,我如何使用 mysql 数据?删除它会使页面崩溃,就好像 php 没有存储结果一样?

4

1 回答 1

0

while当不再有可用行时,该语句将停止。while如果你在循环中表达赋值,PHP只要变量有效(不是NULL),条件就会被认为是真的。否则循环将结束。

于 2013-09-10T04:53:30.813 回答