Quote:
Originally Posted by error_22
Hi,
I was wondering if someone could explain something to me. I have a file called news.php:
news.php
PHP Code:
<?
require ("./includes/db_con.php"); //connects to DB
$sql = "SELECT * FROM `p_news` ORDER BY `p_id` DESC"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_object($result)) { $title = $row->p_title; $content = $row->p_content;
echo $title; echo "<br />"; echo $content; echo "<br /><br />";
} ?>
Then I have another file, index.php:
index.php
PHP Code:
<? require ("./includes/news.php");
echo $title; echo "<br />"; echo $content; echo "<br /><br />"; ?>
My problem is, only the first row shows up, why is that, and how do I fix it?
Thanks in advance
Niklas
|
Because that while loop is already processed before you echo $title and $content...
If you make it like this:
PHP Code:
<?
require ("./includes/db_con.php"); //connects to DB
$sql = "SELECT * FROM `p_news` ORDER BY `p_id` DESC";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
$title = $row->p_title;
$content = $row->p_content;
echo $title;
echo "<br />";
echo $content;
echo "<br /><br />";
}
?>
It should work...