Jump to content

Archived

This topic is now archived and is closed to further replies.

richarddddd

PHP loop for showing all data from MySQL database

Recommended Posts

Dear all,
I need your help!!!! I think my problem is not hard to solve, but I am a newbie and I am designing my first PHP MySQL website. I would like to know how I can make a loop so that PHP is retrieving all the data that is in the MySQL database. Right now, It is looping, but it publish the same content in each field. Please could you help me?

This is the PHP file:

<?php
$query = "SELECT postdate, title, newstextshort, newstextlong, source, sourcehyperlink, category FROM news ORDER BY postdate DESC";
$result= mysql_query($query) or die("unable to find selected job number");

$postdate=mysql_result($result,$i,"postdate");
$title=mysql_result($result,$i,"title");
$newstextshort=mysql_result($result,$i,"newstextshort");
$newstextlong=mysql_result($result,$i,"newstextlong");
$source=mysql_result($result,$i,"source");
$sourcehyperlink=mysql_result($result,$i, "sourcehyperlink");
$category=mysql_result($result,$i,"category");

while ($row = mysql_fetch_array($result))
{

$extract($row);

echo "<table width='75%' border='0'>\n";
echo "<tr>\n";
echo "<td width='15%'><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'>Date:</font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$postdate</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'></font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'><b>$title</b></font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$newstextshort [<a href='$sourcehyperlink'><i>more</i></a>]</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'>Published by:</font></div></td>\n";
echo "<td width='55%'><font size='2' face='Arial, Helvetica, sans-serif'>$source</font></td>\n";
echo "<td width='30%'><font size='2' face='Arial, Helvetica, sans-serif'>Click <a href='$sourcehyperlink'><b>here</b></a> to enter website</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'>Category:</font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$category</font></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<hr>\n";

}
?>

Share this post


Link to post
Share on other sites
Try this

[code]

<?php
$query = "SELECT postdate, title, newstextshort, newstextlong, source, sourcehyperlink, category FROM news ORDER BY postdate DESC";
$result= mysql_query($query) or die("unable to find selected job number");

while ($row = mysql_fetch_assoc($result))
{

$extract($row);

echo "<table width='75%' border='0'>\n";
echo "<tr>\n";
echo "
Helvetica, sans-serif'>Date:
\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$postdate</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'></font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'><b>$title</b></font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$newstextshort [<a href='$sourcehyperlink'><i>more</i></a>]</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'>Published by:</font></div></td>\n";
echo "<td width='55%'><font size='2' face='Arial, Helvetica, sans-serif'>$source</font></td>\n";
echo "<td width='30%'><font size='2' face='Arial, Helvetica, sans-serif'>Click <a href='$sourcehyperlink'><b>here</b></a> to enter website</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'>Category:</font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$category</font></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<hr>\n";

}
?>
[/code]

Share this post


Link to post
Share on other sites
Remove all of this:
[code]$postdate=mysql_result($result,$i,"postdate");
$title=mysql_result($result,$i,"title");
$newstextshort=mysql_result($result,$i,"newstextshort");
$newstextlong=mysql_result($result,$i,"newstextlong");
$source=mysql_result($result,$i,"source");
$sourcehyperlink=mysql_result($result,$i, "sourcehyperlink");
$category=mysql_result($result,$i,"category");[/code]

The extract() does all of that for you within the while loop.

I'm not sure why you're not getting the rest of the results however. The rest of that looks fine.

I will mention, however, that your HTML is totally hosed. You should never use a DIV to align text within a <td> and the <font> tag is depreciated. You should really look into CSS or at least use inline CSS with the "style" attribute of the <td>.

Share this post


Link to post
Share on other sites


I guess it is not working because it needs an associative array for using extract()....

just a thought...

Share this post


Link to post
Share on other sites
Well, I just noticed something else. He is sending an associative array to extract (that's what $row is), but there is a $ before the extract.

Remove the $. extract() is a function, not a variable.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.