0

我想从 mysql 获取新闻(记录)并使用 PHP 在我的站点中显示。这是我的代码(PHP):

<?php
    include ("header.php");
    mysql_connect ("localhost", "root", "") or die (mysql_error());
    mysql_select_db ("geek-art") or die (mysql_error());
    $newsy = mysql_query ("SELECT * FROM newsy order by id desc");

    // With this WHILE loop, I want to display all of my news :)

    while ($rekord = mysql_fetch_array ($newsy)) {

        $id = $rekord['id'];
        $tytul = $rekord['tytul'];
        $tresc = $rekord['tresc'];
        $autor = $rekord['autor'];
        $data = $rekord['data'];
        $obrazek = $rekord['obr-news'];

        echo '<div class="artykul">';
            echo "<h2><a href=\"index.php?news=$id\">".$tytul.'</a></h2>';
            echo '<span>Autor: '.$autor.' Data: '.$data.'</span>';
            echo '<p>'.nl2br($tresc).'</p>';
        echo '</div>';
    }

    // With this WHILE loop, I want to display my news :)

    $newsy_osobne = mysql_query("SELECT * FROM newsy where id=".(int)@$_GET['news']."");
    while($rekordy_osobne = mysql_fetch_array($newsy_osobne)){
        if(isset($_GET['news'])){
            $id = $rekordy_osobne['id'];        
            $tytul = $rekordy_osobne['tytul'];
            $tresc = $rekordy_osobne['tresc'];
            $autor = $rekordy_osobne['autor'];
            $data = $rekordy_osobne['data'];

            echo '<div class="artykul">';
                echo "<h2>".$tytul.'</a></h2>';
                echo '<span>Autor: '.$autor.' Data: '.$data.'</span>';
                echo '<p>'.nl2br($tresc).'</p>';
            echo '</div>';
        }else{
            echo 'Nie znaleziono takiego newsa.';
        }
    }
    include ("footer.php");
?>

我做了一个简短的视频,在那里我展示了我的问题到底是什么:)

4

1 回答 1

1

试试这个,如果您的 id 不是主键,我刚刚将您的查询限制更改为 1,而不是返回多条记录,因此这会将其减少到一条。

// With this WHILE loop, I want to display my news :)

    $newsy_osobne = mysql_query("SELECT * FROM newsy where id='".$_GET['news']."' order by id desc limit 1");
    while($rekordy_osobne = mysql_fetch_array($newsy_osobne)){
        if(isset($_GET['news'])){
            $id = $rekordy_osobne['id'];        
            $tytul = $rekordy_osobne['tytul'];
            $tresc = $rekordy_osobne['tresc'];
            $autor = $rekordy_osobne['autor'];
            $data = $rekordy_osobne['data'];

            echo '<div class="artykul">';
                echo "<h2>".$tytul.'</a></h2>';
                echo '<span>Autor: '.$autor.' Data: '.$data.'</span>';
                echo '<p>'.nl2br($tresc).'</p>';
            echo '</div>';
        }else{
            echo 'Nie znaleziono takiego newsa.';
        }
    }
于 2013-09-20T12:53:02.110 回答