richarddddd Posted May 18, 2006 Share Posted May 18, 2006 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/9914-php-loop-for-showing-all-data-from-mysql-database/ Share on other sites More sharing options...
samshel Posted May 18, 2006 Share Posted May 18, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/9914-php-loop-for-showing-all-data-from-mysql-database/#findComment-36854 Share on other sites More sharing options...
ober Posted May 18, 2006 Share Posted May 18, 2006 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>. Quote Link to comment https://forums.phpfreaks.com/topic/9914-php-loop-for-showing-all-data-from-mysql-database/#findComment-36855 Share on other sites More sharing options...
samshel Posted May 18, 2006 Share Posted May 18, 2006 I guess it is not working because it needs an associative array for using extract()....just a thought... Quote Link to comment https://forums.phpfreaks.com/topic/9914-php-loop-for-showing-all-data-from-mysql-database/#findComment-36857 Share on other sites More sharing options...
ober Posted May 18, 2006 Share Posted May 18, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9914-php-loop-for-showing-all-data-from-mysql-database/#findComment-36863 Share on other sites More sharing options...
samshel Posted May 18, 2006 Share Posted May 18, 2006 Bingo !!Nice observation i must admit !! Quote Link to comment https://forums.phpfreaks.com/topic/9914-php-loop-for-showing-all-data-from-mysql-database/#findComment-36868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.