简体   繁体   English

While循环显示结果3次

[英]While loop displaying result 3 times

Basicly I'm trying to make a simple news feed but I'm stuck at the moment as my while loop display the result 3 times, why is this? 基本上,我试图制作一个简单的新闻提要,但是由于我的while循环将结果显示3次,因此我被卡住了,这是为什么? :/ :/

<?php 
$sql ="SELECT
            *
        FROM
            news,
            admins";
$result = mysql_query($sql);

if(!$result)
{
    echo 'Error while selecting from database. Please contact the administration team';
} else {
    while($row = mysql_fetch_assoc($result))
    {
        echo '
        <div class="content_news">
            <h1>' . $row['news_name'] . '</h1>
            <p style="font-size:12px;">Posted by <b>' . $row['admin_name'] . '</b> on ' . $row['news_date'] . '
            <p>' . $row['news_description'] . '</p>
            <a href="' . $row['news_link'] . '">read more</a>
        </div>
        ';
    }
}

?> ?>

If you'd like to see what I am talking about: http://freewallpaperblog.com/freshrp/ 如果您想了解我在说什么: http : //freewallpaperblog.com/freshrp/

Ignore the last 2(those are static html not php) 忽略最后2个(那些是静态html而不是php)

You either have 3 admins or 3 rows of news. 您要么拥有3个管理员,要么拥有3行新闻。 Your query makes a direct multiplication between tables. 您的查询使表之间直接相乘。 Try "left join" instead... 尝试“左加入” ...

your query selects data from 2 tables ( news , admins ) so it joins every row of 1st table with every row of 2nd table 您的查询从2个表( newsadmins )中选择数据,因此它将第一表的每一行与第二表的每一行连接起来

SELECT * FROM news, admins

i recommend you to use following query 我建议您使用以下查询

SELECT news.*, admins.admin_name FROM news
INNER JOIN admins ON news.admin_id = admins.id

where admin_id is your correct column name 其中admin_id是您正确的列名

SELECT * FROM news
INNER JOIN admins ON admins.id = news.adminid

Or whatever adminid is in the news table. 或新闻表中的任何adminid。

Try the following query: 请尝试以下查询:

 SELECT
                *
            FROM
                news
    Inner join admins on news.admin_id = admins.id

You made no JOIN statement in your SQL, as someone else has already commented on in your question. 您在SQL中未执行JOIN语句,因为其他人已经在您的问题中进行了评论。 It would help if you posted the associated fields you're grabbing, but based on your $row keys, my best guess is the following should work for you (but I can't promise it will without knowing how your database is designed, I can only infer from the variable names): 如果您发布要获取的关联字段会有所帮助,但是根据您的$ row键,我的最佳猜测是以下内容对您有用(但是如果不知道数据库的设计,我无法保证会做到这一点,我只能从变量名推断):

$sql = "SELECT news.name, news.date, news.description, news.link, admins.name"
. "FROM news"
. "INNER JOIN admins"
. "ON news.name=admins.name"

References: 参考文献:

http://www.w3schools.com/sql/sql_join_inner.asp http://www.w3schools.com/sql/sql_join_inner.asp

http://www.w3schools.com/sql/sql_join_left.asp http://www.w3schools.com/sql/sql_join_left.asp

http://dev.mysql.com/doc/refman/5.0/en/join.html http://dev.mysql.com/doc/refman/5.0/en/join.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM