php
2014-01-16 40 views 0 likes 
0

有什么办法只用一个while循环从两个表中获取数据?如何在一个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>"; 
?> 
+1

相信你能,一个可以完成比其他早期所以你必须检查过 – slash197

+0

结合两种查询,通过加入或工会...这取决于在你的数据库结构上。 – user1844933

+0

是的,尝试使用'JOIN',但两个表都需要具有像'id'一样的字段。 – rray

回答

0

自己的工作风格是不正确的使用 然后使连接查询 让生活容易:)

感谢

加入两个表
0

做结果集变量usi纳克内连接或多个选择 Select A.date1, B.date2 from table1 A, table2 B where ... 那么你就可以当作单个ResultSet 或者你可以使用“之间”的句子,如果你正在寻找的日期之间的数据 Select sales from table where dates between 'date1' and 'date2'

+0

你可以给我完整的例子吗? – user220095

+0

从pm A,pm_reply B中选择A. *,B. *,其中A.send_to ='$ _ GET [name]'和B.send_to ='$ _ GET [name]' 这将提取每个表中的所有字段,确保只提取你需要的字段 –

0

没有什么能够阻止你写

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

但是这个代码“闻香”

相关问题