首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用2个while循环从2个表获取数据

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

Stack Overflow用户
提问于 2011-02-19 18:09:47
回答 1查看 2.1K关注 0票数 0

我正在尝试从用户数据库中获取一个id,这样我就可以链接到about.php?id=$id1,我使用了2个while循环来实现。我最终得到了我想要的,about.php?id=$id1,但是有重复的条目...

这是代码。

代码语言:javascript
复制
<?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");
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-19 22:14:58

我不能完全理解你的问题,但是SELECT DISTINCT column_name FROM table对你有帮助吗?它只从表中选择不同的行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5050151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档