簡體   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