繁体   English   中英

如何在while循环中从两个表中获取数据?

[英]How to fetch data from two tables in one while loop?

有什么方法可以只使用一个while循环从两个表中获取数据?

这是代码:

<?php

$sql=mysql_query("select *from pm where send_to='$_GET[name]' limit $offset, $rowsperpage");

$sql2=mysql_query("select *from pm_reply where send_to='$_GET[name]' limit $offset, $rowsperpage");

$start_from = $offset + 1; 
echo "<ol start='$start_from'>";
while($rows=mysql_fetch_assoc($sql))

{

echo"<li>From: <a href='profile.php?name=$rows[send_by]'>$rows[send_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows[subject]'>$rows[subject]</a></li>"; 

echo "<hr>";
echo "<br>";
}

while($rows2=mysql_fetch_assoc($sql2))
{
echo"<li>From: <a href='profile.php?name=$rows2[replied_by]'>$rows2[replied_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows2[subject]'>$rows2[subject]</a></li>"; 

echo "<hr>";
echo "<br>"
}

echo "</ol>";
?>

您的工作风格不正确使用 连接两个表然后进行连接查询使生活变得轻松:)

谢谢

使用内部Select A.date1, B.date2 from table1 A, table2 B where ...或多项选择来执行结果集变量Select A.date1, B.date2 from table1 A, table2 B where ...然后Select A.date1, B.date2 from table1 A, table2 B where ...可以将其视为单个结果集;或者,如果使用在日期之间搜索数据Select sales from table where dates between 'date1' and 'date2'

没有什么可以阻止你写

while(($rows=mysql_fetch_assoc($sql)) OR ($rows2=mysql_fetch_assoc($sql2)))
{
    if ($rows)
    {
        // ...
    }
    if ($rows2)
    {
        // ...
    }
}

但是这段代码“闻起来”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM