while循环没有显示任何内容

时间:2014-01-28 13:01:07

标签: php while-loop

我正在尝试让我的页面显示我在数据库中的所有新闻。 但在我的网站上没有任何显示没有错误让我知道如果有什么不对,只有一个空白页面。 我使用了var_dump()并返回了我请求的所有数据。

这是我的页面代码

<?php
ini_set('display_errors', 1);
header("Content-type: text/xml");
include("includes/database.php");
global $NEWS;
$str = '<?xml version="1.0" encoding="UTF-8"?>';
$str.= '<rss version="2.0">';
$str.='<channel>';
$sql = "SELECT * FROM news";

$result = mysql_query($sql) or die ($sql."".mysql_error());

while($row = mysql_fetch_object($result)){
    $str.= '<item>';
    $str.= '<title>'.$row->title.'</title>';
    $str.= '<description>'.$row->content. '</description>';
    $str.= '</item>';
}

$str .='</channel>';
$str .='</rss>';
echo $str;

2 个答案:

答案 0 :(得分:3)

   <?php
  ini_set('display_errors', 1);
  header("Content-type: text/xml"); 
include("includes/database.php");
global $NEWS;
$str = '<?xml version="1.0" encoding="UTF-8"?>';
$str.= '<rss version="2.0">';
$str.='<channel>';
$sql = "SELECT * FROM news";

$result = mysql_query($sql) or die ($sql."".mysql_error());

foreach($row in mysql_fetch_object($result)){
    $str.= '<item>';
    $str.= '<title>'.$row->title.'</title>';
    $str.= '<description>'.$row->content. '</description>';
    $str.= '</item>';
}

$str .='</channel>';
$str .='</rss>';
echo $str;

答案 1 :(得分:0)

检查数据库连接:

mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
相关问题