CodeMama Posted November 28, 2008 Share Posted November 28, 2008 I have to output 3 random text blurbs from one table and count how many times they appear, I can get one, but everything I have tried to make 3 random results just seems to cause an endless loop and timeout, and I have figured the counting part out yet either (big big newbie here) here is my code so far: <?php include 'inc/dbconnOpen.php'; ini_set('error_reporting', E_ALL); ini_set('display_errors', true); $sql ="select * from textads ORDER BY RAND() LIMIT 0,1"; /* query */ $result= mysql_query($sql); $num=mysql_num_rows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $href=mysql_result($result,$i,"href"); $blurb=mysql_result($result,$i,"blurb"); $title=mysql_result($result,$i,"title"); echo "<table border= 1 height=90px width=468px bgcolor=#cccccc><tr><td>$title<br>$blurb<br> <A HREF='$href'>$href</a></td><td>$title<br>$blurb<br> <A HREF='$href'>$href</a></td><td>$title<br>$blurb<br> <A HREF='$href'>$href</a></td></tr></table>"; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/134639-solved-3-random-results-from-one-table/ Share on other sites More sharing options...
rhodesa Posted November 28, 2008 Share Posted November 28, 2008 change your query to: $sql ="select * from textads ORDER BY RAND() LIMIT 0,3"; /* query */ Link to comment https://forums.phpfreaks.com/topic/134639-solved-3-random-results-from-one-table/#findComment-701021 Share on other sites More sharing options...
CodeMama Posted November 28, 2008 Author Share Posted November 28, 2008 Hi, I actually had tried that but it makes 3 tables with still the same text blurb in each column in the table, so possibly I am outputting the echo the wrong way? Link to comment https://forums.phpfreaks.com/topic/134639-solved-3-random-results-from-one-table/#findComment-701039 Share on other sites More sharing options...
rhodesa Posted November 28, 2008 Share Posted November 28, 2008 um...noticed you have a mysql_close() WAY to early in the script. frankly, you don't need that at all as PHP will do that automatically try: <?php include 'inc/dbconnOpen.php'; ini_set('error_reporting', E_ALL); ini_set('display_errors', true); $sql ="select * from textads ORDER BY RAND() LIMIT 0,3"; /* query */ $result= mysql_query($sql); echo "<b><center>Database Output</center></b><br><br>"; echo "<table border= 1 height=90px width=468px bgcolor=#cccccc><tr>"; while ($row = mysql_fetch_assoc($result)) { echo "<td>{$row['title']}<br>{$row['blurb']}<br><A HREF=\"{$row['href']}\">{$row['href']}</a></td>\n"; } echo "</tr></table>"; ?> Link to comment https://forums.phpfreaks.com/topic/134639-solved-3-random-results-from-one-table/#findComment-701041 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.