Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用2个while循环从2个表中获取数据_Php_Mysql - Fatal编程技术网

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

Php 使用2个while循环从2个表中获取数据,php,mysql,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

我试图从用户数据库中获取一个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");
?>

无法完全理解您的问题,但
从表中选择不同的列名是否有帮助?它只从表中选择不同的行。

您能更具体地说明哪个查询显示重复的结果吗?在php之外运行查询是否会得到重复的结果?
SELECT*FROM blogdata,其中id='$id'
会给我重复的结果。我已经设法使用if语句来解决这个问题,但如果我添加更多的用户,这将是一个难题。基本上,它只是'if($user==Hugo){echo“about?id=1”;else echo“about?id=2”;你在php之外运行查询时会得到重复的结果吗?不,我没有,谢谢,我把它从while循环中去掉,然后把
$sql1=mysql_查询(“从用户选择id”);$row1=mysql_num_行($sql1);$id1=$row['id']
我还没有尝试过,但我认为它不起作用,因为我需要从数据库中选择标题、作者、电子邮件、日期和类别。您可以
从表中选择不同的标题、作者、电子邮件、数据、类别来满足您的需要。它将获取所有行,其中至少包含指定列值之间的一个差异纳什。