在php中使用2 while循环和move next

ia2d9nvy  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(431)

有谁能帮我使用2 while循环条件和2 move next?

<?
$condition1=$core_db->Execute("select Husbandname, age, marriedto from 
husbands");
$condition2=$core_db->Execute("select age, marriedto from wifes where 
wifename=?", array($condition1->fields[2]));;

echo 'any';
while(!$condition1->EOF){   
    echo 'any';
    $condition1->MoveNext();
};
?>

我上面的示例显示了表hursons中的丈夫姓名和年龄,现在我想在同一个循环中使用movenext从wifes表中获取妻子数据,以提供所有成员的数据。
你知道吗?

nr9pn0ug

nr9pn0ug1#

查看联接:https://www.w3schools.com/sql/sql_join.asp
您的基本意思是您需要一个sql语句和一个while循环(如果我理解正确的话)

<?
$condition=$core_db->Execute("select * FROM table1 
INNER JOIN table2 ON table2.id = table1.id");

echo 'any';
while(!$condition->EOF){   
    echo 'any';
    $condition->MoveNext();
};

?>

更新:
根据您的更新:

$condition1=$core_db->Execute("select Husbandname, age, 
marriedto from husbands");
$condition2=$core_db->Execute("select age, 
marriedto from wifes where wifename=?", array($condition1->fields[2]));

我会这样做(sql方式):

$condition = $core_db->Execute("select husbands.Husbandname, 
husbands.age, husbands.marriedto, wifes.id, wifes.age, wifes.marriedto from 
husbands INNER JOIN husbands.marriedto = wifes.id");

while(!$condition->EOF){   
    echo 'any';
    $condition->MoveNext();
};

如果你想用php而不是sql来实现这一点,你可以这样做(我推荐sql-way)

<?
$condition1=$core_db->Execute("select Husbandname, age, 
marriedto from husbands");

echo 'any';
while(!$condition1->EOF){   
    $condition2=$core_db->Execute("select age, 
    marriedto from wifes where wifename=?", array($condition1->fields[2]));

    echo 'any';
    $condition1->MoveNext();
};

?>
taor4pac

taor4pac2#

你的意思是这样的:

<?
  $condition1=$core_db->Execute("select ANY from Any");
  $condition2=$core_db->Execute("select ANY from Any");

        while(!$condition1->EOF || !condition2->EOF){   
            if (!$condition1->EOF) {
                // Code for condition1...
                $condition1->MoveNext();
            }
            if (!$condition2->EOF) {
                // Code for condition2...
                $condition2->MoveNext();
            }
        };
?>

但这通常不是正确的方法(在一个循环中处理查询)

相关问题