dousha2020 2013-09-20 12:50
浏览 26

使用PHP在新站点上显示MySQL的记录? [关闭]

I want to fetch a news (record) from mysql and display in my site using PHP. Here is my code(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");
?>

And I have done a short Video, where I show, what exactly is my problem :)

  • 写回答

1条回答 默认 最新

  • duanqiang3925 2013-09-20 12:53
    关注

    try this, I've just changed your query limit to 1 if your id is not primary key than it is possibile that multiple records are returned so this will reduce it to one.

    // 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.';
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序