使用2 while循环从2个表中获取数据

时间:2011-02-19 10:09:47

标签: php mysql

我正在尝试从用户数据库中获取一个id,以便我可以链接到about.php?id = $ id1并且我正在使用2 while循环来执行此操作。我最终得到了我想要的东西,about.php?id = $ id1但是有重复的条目......

这是代码。

<?php
require("connect.php");
require("header.php");



$max = 5; //amount of articles per page. change to what to want

$p = $_GET['p'];

if(empty($p))

{

$p = 1;

}

$limits = ($p - 1) * $max; 

//view the news article!
$id = isset($_GET['id']) ? $_GET['id'] : false;
if($id && is_numeric($id)){

$id = $_GET['id'];

$sql = mysql_query("SELECT * FROM blogdata WHERE id = '$id'");




while($r = mysql_fetch_array($sql))

{
echo $total;
$id = $r['id'];
$date = $r['date'];
$title = $r['title'];
$content = $r['content'];
$email = $r['author_email'];
$cat = $r['category'];
$author = $r['author'];

$query1 = mysql_query("SELECT * FROM users");
while ($row1 = mysql_fetch_array($query1)){
$id1 = $row1['id'];

echo "<center>
        <table border='0' width='100%' cellspacing='10'>
        <tr>
        <td width='20%' valign='top'><div class='title'><a href=\"?id=$id\">$title</a></div>
                <div class='info'><i>$date</i><br />
                By <a href='$id1'>$author</a><br />
                $cat</div>
        </td>
        <td width='80%' valign='top'>";
        echo nl2br($content); 
        echo "</td>
        </tr>
        </table>
        <hr />
        <a href='index'>&larr; Rewind.</a>
        </center>";

}
}



}else{



//view all the news articles in rows

$sql = mysql_query("SELECT * FROM blogdata ORDER BY id DESC LIMIT ".$limits.",$max") or die(mysql_error());

//the total rows in the table

$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM blogdata"),0);       

//the total number of pages (calculated result), math stuff...

$totalpages = ceil($totalres / $max); 

while($r = mysql_fetch_array($sql))

{

$id = $r['id'];
$date = $r['date'];
$title = $r['title'];
$content = $r['content'];
$email = $r['author_email'];
$cat = $r['category'];
$author = $r['author'];

$query1 = mysql_query("SELECT * FROM users");
while ($row1 = mysql_fetch_array($query1)){
$id1 = $row1['id'];

echo "<center>
        <table border='0' width='100%' cellspacing='10'>
        <tr>
        <td width='20%' valign='top'><div class='title'><a href=\"?id=$id\">$title</a></div>
                <div class='info'><i>$date</i><br />
                By <a href='$id1'>$author</a><br />
                $cat</div>
        </td>
        <td width='80%' valign='top'>";
        echo nl2br($content); 
        echo "</td>
        </tr>
        </table>
        <hr />
        </center>";

}
}
//close up the table

echo "</tr></table><center>";

$page = $_GET['p'];

for($i = 1; $i <= $totalpages; $i++)
{ 
    if ( $page == $i ) {
        echo "<b>$i</b>";
    } else {
        echo "<a href='?p=$i'>$i</a>";
    }
    if ($i < $totalpages)
        echo " <font color=\"#666\">&bull;</font> ";
}
}


echo "<br />";
require("footer.php");
?>

1 个答案:

答案 0 :(得分:0)

无法完全解答您的问题,SELECT DISTINCT column_name FROM table会帮助您吗?它只从表中选择不同的行。

相关问题