我想从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");
?>
我做了一个简短的Video,在那里我展示了我的问题究竟是什么:)
答案 0 :(得分:1)
试试这个,我刚刚将你的查询限制改为1,如果你的id不是主键,那么可能会返回多个记录,所以这会把它减少到一个。
// 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.';
}
}