我有这样的PHP代码:
$q = $db->query("SELECT * FROM tabe1_data WHERE status='1' ORDER BY id DESC LIMIT 10");
$cr_onr = array();
while($row = $db->fetchAll($q))
{
$cr_onr[] = $row;
}
?>
<?php foreach($cr_onr as $new_cr_onr):?>
<?php $crinfo = $db->fetchOne("SELECT * FROM tabe2_data WHERE id ='".$new_cr_onr['op_id']."'");?>
<p><?=$new_cr_onr['username'];?> has been complete <?=$crinfo['op_amount'];?></p>
<?php endforeach;?>
我需要将$crinfo = $db->
从“foreach”中取出来,然后才能在“foreach”之前
我怎么能这样做?
谢谢。
答案 0 :(得分:1)
您可以加入表格而不是查询很多:
$q = $db->query("
SELECT
*
FROM
tabe1_data
INNER JOIN
tabe2_data ON tabe1_data.op_id = tabe2_data.id
WHERE
tabe1_data.status='1'
ORDER BY
tabe1_data.id DESC
LIMIT 10
");
$cr_onr = array();
while($row = $db->fetchAll($q))
{
$cr_onr[] = $row;
}
?>
<?php foreach($cr_onr as $new_cr_onr):?>
<p><?=$new_cr_onr['username'];?> has been complete <?=$new_cr_onr['op_amount'];?></p>
<?php endforeach;?>
答案 1 :(得分:1)
单个加入查询?
SELECT tabe2_data.*
FROM tabe2_data
LEFT JOIN tabe1_data ON tabe1_data.op_id = tabe2_data.id
WHERE tabe1_data.status='1'