-1

我有这样的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”

请问我该怎么做?

谢谢你。

4

2 回答 2

1

单连接查询?

SELECT tabe2_data.*
FROM tabe2_data
LEFT JOIN tabe1_data ON tabe1_data.op_id = tabe2_data.id
WHERE tabe1_data.status='1'
于 2013-03-21T20:51:41.933 回答
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;?>
于 2013-03-21T20:49:41.393 回答