php - 使用php循环遍历mysql表,为什么elseif语句不起作用?

标签 php mysql sql if-statement while-loop

我有一组页面,它们采用两个边界(来自用户输入),这会产生一个值,然后将其放入 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 没有存储结果一样?

最佳答案

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

关于php - 使用php循环遍历mysql表,为什么elseif语句不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710911/

相关文章:

php - REGEX 永远不会与 .htaccess 中的 Origin 匹配

sql - 如何在 mysql 中获取平均字符串长度?

mysql - 使用 AND(或)根据多行值进行选择

JAVAFX:从数据库加载图像

php - PDO 和类给出 fatal error : Call to a member function query()

php - Mysql查询如何返回空值组?

php - 如何获取总数量并将其发送到另一个表(Laravel)

php - MySQL 内部连接返回同一行的倍数

phpMyAdmin float 禁用轮数

sql - Postgres 10 sql 如何在没有所有列的情况下进行区分